2019-04-22 | spring | UNLOCK

Spring Boot - Building RESTful Web Service


学习文档:
https://www.tutorialspoint.com/spring_boot/spring_boot_building_restful_web_services.htm

添加依赖

为了构建RESTful Web Services,我们需要添加spring-boot-starter-web依赖。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.restfulapio</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.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>

实现控制器

  • @RestController

    使用@RestController注解来定义RESTful web 服务,它提供JSON,XML等响应数据格式。

  • @RequestMapping(value=””)

    使用@RequestMapping来定义Request的url入口

  • @RequestBody

    使用@RequestBody来定义获取request请求中的content

  • @PathVariable(“”)

    使用@PathVariable来获取用户自定义变量或动态变量

  • @RequestParam

    使用@RequestParam来获取request请求的url中的参数

代码案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.restfulapio.demo.controller;

import com.restfulapio.demo.bean.Product;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.misc.Request;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ProductServiceController {
//模拟数据
public static Map<String, Product> productRepo = new HashMap<>();
static{
Product honey = new Product();
honey.setId("1");
honey.setName("Honey");
productRepo.put(honey.getId(), honey);

Product almond = new Product();
almond.setId("2");
almond.setName("Almond");
productRepo.put(almond.getId(), almond);

}

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProduct(){
return new ResponseEntity<Object>(productRepo.values(), HttpStatus.OK);
}

@RequsetMapping(value = "/products",method= RequestMethod.POST)
public ResponseEntity<Object> createProduct(@RequestBody Product product){
productRepo.put(product.getId(),product);
return new ResponseEntity<>("Product is created successfully",HttpStatus.CREATED);
}
@RequestMapping(value = "/products/{id}",method = RequestMethod.PUT)
public ResponseEntity<Object> updateProduct(@PathVariable("id") String id,@RequestBody Product product){
productRepo.remove(id);
product.setId(id);
productRepo.put(id, product);
return new ResponseEntity<>("Product is updated successfully",HttpStatus.OK);
}

@RequestMapping(value = "/products/{id}",method = RequestMethod.DELETE)
public ResponseEntity<Object> delete(@PathVariable("id") String id){
productRepo.remove(id);
return new ResponseEntity<>("Product is deleted successfully",HttpStatus.OK);
}
}

测试

使用Postman进行测试前,需要添加Content-Type:application/json;charset:utf-8;

GET API TEST

POST API TEST

PUT API TEST

DELETE API TEST

评论加载中