1. 程式人生 > >SpringBoot學習筆記(1)------SpringBoot HelloWord!

SpringBoot學習筆記(1)------SpringBoot HelloWord!

SpringBoot主要有以下特點:

1.內建Tomcat,Jetty等伺服器
2.不需要太多依賴,提供’starter’來簡化你的maven配置
3.將盡可能的自動注入配置
4.不需要xml配置,不會生成多與程式碼

1.建立專案

我們以Eclipse為例,新建一個mavenweb專案,新增依賴:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion> <groupId>com.springBoot</groupId> <artifactId>springBootDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springBootDemo Maven Webapp</name> <url>
http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId
>
junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 新增servlet支援 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <!-- 新增jtl支援 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>

spring-boot-maven-plugin裡提供大量方便的依賴

2.建立一個簡單的JavaBean接受資料

public class HelloWorldBean {

    private final long id;
    private final String content;

    public HelloWorldBean(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

3.建立控制層

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BeanController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/helloWorld")
    public HelloWorldBean greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new HelloWorldBean (counter.incrementAndGet(),
                            String.format(template, name));
    }
}

4.啟動SpringBoot服務

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

所有程式碼編寫完成後以java應用程式的方式執行Application類,@SpringBootApplication 包括許多註解,使用@SpringBootApplication 可以更方便。在Application的main() 方法中呼叫SpringApplication.run() 方法啟動應用程式,會看到以下介面。
這裡寫圖片描述
然後在瀏覽器輸入http://localhost:8080/helloWorld
你會看到以下內容:

{“id”:2,”content”:”Hello, World!”}

{“id”:3,”content”:”Hello, springBoot!”}

springBoot返回的是一個json格式的字串,springBoot使用jackson庫自動將helloWord例項轉化成Json。

ok!SpringBoot版的HelloWorld已經實現了!