1、Spring Boot构建Hello World步骤
(1)新建一个Maven Java工程
(2)在pom.xml文件中添加Spring Boot Maven依赖
(3)编写启动类
(4)运行程序
1.1 构建工程
这个步骤很简单,用idea创建,可以命名工程名为spring-boot-hello-world。
1.2 pom.xml配置
<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.demo</groupId>
<artifactId>spring-boot-hello-world</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 使用 java 1.8 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 引入spring-boot-starter-paren以后在申明其它spring boot的dependency的时候就不需要version了 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<!--
spring-boot-start-web包含了spring WebMvc和tomcat等web开发的特性。
Spring WebMvc框架会将Servlet容器里收到的HTTP请求根据路径分发给对应的@Controller类进行处理,
@RestController是一类特殊的@Controller,它的返回值直接作为HTTP Response的Body部分返回给浏览器。
-->
<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>
<!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动。如果使用maven的spring-boot:run的话是不需要此配置的。 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</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>
1.3 代码
1.3.1 Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 使应用程序可执行
*
* 虽然可将此服务打包为war部署到外部服务器上,但是可以利用下面更简单的方法是应用程序可执行。
* 我们可以在一个可执行的jar文件中打包所有的内容,由一个main()方法驱动。可以使用spring的支持将tomcat容器嵌入http运行时,而不用部署到外部实例。
*/
@SpringBootApplication
/**
* @SpringBootApplication 申明让spring boot自动给程序进行必要的配置,它默认等同于 @Configuration , @EnableAutoConfiguration 和 @ComponentScan
*/
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.3.2 Greeting.java
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
1.3.3 GreetingController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
/**
* @RestController 返回json字符串的数据,直接可以编写RESTFul的接口
*
* 在spring boot中来用@RestController来标注返回json给的数据
*
* 其实在其实Spring Boot也是引用了JSON解析包jackson-databind,它是包含在spring-boot-starter-parent当中的,
* 因此自然就可以在对象上使用Jackson提供的json属性的注解。
*/
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
1.4 运行程序
运行程序,有两种方式:
第一种方式特别简单,在Application.java中直接右键Run Application.java解即可。
第二种方式Maven build在Commond Line里输入spring-boot:run,然后Apply,最后Run。
浏览器访问:http://localhost:8080/greeting
返回结果:{“id”:3,”content”:”Hello, World!”}