最簡(jiǎn)單的SpringBoot應(yīng)用
首先使用maven添加依賴
POM.XML
<?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>com.tw.josaber</groupId>
<artifactId>todo-list-api</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
添加Controller
HelloController.java
package com.tw.josaber;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/")
@ResponseBody
String hello() {
return "Hello World!";
}
}
Spring boot 啟動(dòng)入口
Application.java
package com.tw.josaber;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
啟動(dòng)應(yīng)用
mvn spring-boot:run
訪問(wèn)http://localhost:8080
為了進(jìn)一步地學(xué)習(xí)Spring boot,我們來(lái)實(shí)現(xiàn)TodoListApi
GitHub地址: TodoListApi
結(jié)果如下
GET
$ curl -X GET "http://localhost:8080/todoitems" -H "accept: application/json"
...
{"items":[{"id":1,"text":"finish todo list","done":false,"timestamp":"2011-01-21T11:33:21Z"},{"id":2,"text":"finish the homework","done":true,"timestamp":"2011-01-21T11:33:21Z"},{"id":3,"text":"This is for test create.","done":true,"timestamp":"2017-01-20T16:23:05Z"}]}%
POST
$ curl -X POST "http://localhost:8080/todoitems" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"text\": \"string\"}"
...
{"status":201,"message":"Create Todo Item Successfully!","todoItem":{"id":4,"text":"string","done":false,"timestamp":"2017-09-25T21:46:25Z"}}%
PUT
$ curl -X PUT "http://localhost:8080/todoitems/1" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"text\": \"string\", \"done\": true}"
...
{"status":200,"message":"Update Todo Item Successfully!","todoItem":{"id":1,"text":"string","done":true,"timestamp":"2017-09-25T21:52:34Z"}}%
$ curl -X PUT "http://localhost:8080/todoitems/5" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"text\": \"string\", \"done\": true}"
...
{"status":404,"message":"The Todo Item Is Not Found!","todoItem":null}%
DELETE
$ curl -X DELETE "http://localhost:8080/todoitems/1" -H "accept: application/json"
$ curl -X DELETE "http://localhost:8080/todoitems/100" -H "accept: application/json"
...
{"status":404,"message":"Fail to Delete Todo Item!","todoItem":null}%
參考
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/