javascript
Spring Boot文档阅读笔记-构建Restful风格的WebService
Maven代碼如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.it1995</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>服務端通過處理/greeting的get方法,接收name這個string參數,返回200的json狀態。
body返回如下:
{"id": 1,"content": "Hello, World!" }id這個域是greetting的主鍵,這個值是唯一的。content里面是問候語。
Greeting.java代碼如下:
package cn.it1995.demo;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;} }程序通過適用JaksonJson庫,將Greeting自動整理成json,Jackson默認被包含在web starter的jar包中,也就是這個jar包里面:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>使用@RestController創建Rest風格的WebService,GreetingController.java監聽了/greeting的GET方法,代碼如下:
package cn.it1995.demo;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.atomic.AtomicLong;@RestController public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@GetMapping("/greeting")public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name){return new Greeting(counter.incrementAndGet(), String.format(template, name));} }@GetMapping:代表適用HTTP的get方法,參數為/greeting代表http里面的url為/greeting,等同于@RequestMapping(method=GET)。
@RequestParam:為需要傳入的參數,defaultValue為設置默認值。
@RestController:等同于@Controller加@ResponseBody
這里Greeting對象能夠轉換為json多虧了Jackson2。Spring的MappingJackson2HttpMessageConverter會將Greeting轉換為JSON。
?
程序運行截圖如下:
?
總結
以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-构建Restful风格的WebService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux笔记-centos7替换yum
- 下一篇: SQL笔记-通过构建索引表方便数据库管理