SpringBoot - 1 - New Spring Star
2017-08-14 本文已影响92人
ThingLin
工具:Spring Tool Suite 3.8.4
Spring Boot:极简化了一个web项目的配置。
1.创建SpringBoot模板工程


SB版本、资源整合

2.生成springboot模板整合了web的工程
工程结构包名出了点小意外,手动修改回想要的就可以,还有是测试的配置文件目录需要自己手动添加下(src/test/resources)。
application.properties文件是空空如也什么也没有的。

POM.xml,自动依赖了springboot
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.thinglin.springboot.demo</groupId>
<artifactId>cn.thinglin.springboot.demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>cn.thinglin.springboot.demo</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
依赖包中已经加入了web工程开发所需要的大部分jar包


3.基本使用
Application.java类是项目入口
package cn.thinglin.springboot.demo;
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);
}
}
@SpringBootApplication注解集成了很多注解,包括自动配置

写的controller、service等使用了注解让spring扫描的类需要写在Application类同级包或更多级都可以但不能不在这个包之前,如我写一个Rest风格的controller
在Application所在包下新建一级ctrl包并创建一个java类TestRestCtrl.java

TestRestCtrl.java
package cn.thinglin.springboot.demo.ctrl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController /* RestController直接回JSON格式的ResponseBody */
@RequestMapping("/test")
public class TestRestCtrl {
@RequestMapping("test.json")
public String test(){
return "test Spring Boot";
}
}
写完直接在Application运行

监听了8080端口

访问成功但是没看到rest的风格

4.Rest
增加一个Pojo,一个普通的Javabean

package cn.thinglin.springboot.pojo;
import java.util.ArrayList;
public class TestPojo {
private String uid;
private String desc;
private int ret;
private ArrayList<String> data;
public TestPojo() {
super();
}
public TestPojo(String uid, String desc, int ret, ArrayList<String> data) {
super();
this.uid = uid;
this.desc = desc;
this.ret = ret;
this.data = data;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public ArrayList<String> getData() {
return data;
}
public void setData(ArrayList<String> data) {
this.data = data;
}
}
TestRestCtrl.java新增加一个testRest.json接口
package cn.thinglin.springboot.demo.ctrl;
import java.util.ArrayList;
import java.util.UUID;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.thinglin.springboot.pojo.TestPojo;
@RestController /* RestController直接回JSON格式的ResponseBody */
@RequestMapping("/test")
public class TestRestCtrl {
@RequestMapping("test.json")
public String test(){
return "test Spring Boot";
}
@RequestMapping("testRest.json")
public TestPojo testRest(){
ArrayList<String> data = new ArrayList<String>();
data.add("元素1");
data.add("元素2");
return new TestPojo(UUID.randomUUID().toString(), "SB真是好东西",200,data);
}
}
自动把对象转化成JSON格式数据了,如果数据是Map格式也会被直接转成键值对的JSON对象
