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!”}
你可以带一个name参数 http://localhost:8080/helloWorld?name=springBoot
{“id”:3,”content”:”Hello, springBoot!”}
springBoot返回的是一个json格式的字符串,springBoot使用jackson库自动将helloWord实例转化成Json。
ok!SpringBoot版的HelloWorld已经实现了!