Spring Boot REST(一)核心接口

Spring Boot REST(一)核心接口

Spring 系列目录(http://www.javashuo.com/article/p-hskusway-em.html)html

SpringBoot REST 系列相关的文章:java

  1. SpringBoot REST(一)核心接口
  2. SpringBoot REST(二)源码分析

1、Spring 中与 REST 相关的注解

Spring 有如下相关的注解:spring

## 定义相关
  |-- @Controller
  |-- @RestController

## 映射相关
  |-- @RequestMapping
  |-- @GetMapping
  |-- @PostMapping
  |-- @PathVariable

## 请求相关
  |-- @RequestBody
  |-- @RequestParam
  |-- @RequestHeader
  |-- @CookieValue
  |-- RequestEntity

## 定义相关
  |-- @ResponseBody
  |-- ResponseEntity

1.1 RestController

@RestController 至关于 @ResponseBody 和 @Controller 的组合。json

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    @AliasFor(annotation = Controller.class)
    String value() default "";
}

使用以下,能够省略 @ResponseBody 注解:app

@RestController
public class HelloController {

    @RequestMapping("/v1/test")
    public String test1() {
        return "test";
    }
}

1.2 请求路径映射相关的注解

// @PathVariable 解析请求路径中的参数
@RequestMapping("/v1/{city_id}/{user_id}")
public String test2(@PathVariable("city_id") String cityId, 
    @PathVariable(value = "user_id") String userId) {
    return cityId;
}

1.3 请求相关的注解

// 1. @RequestParam 请求的参数
@GetMapping("/v1/test4")
public String test4(@RequestParam(name = "user_id", defaultValue = "2") String userId) {
    return userId;
}

// 2. @RequestHeader 请求头中的参数
@GetMapping("/v1/get_header")
public String test6(@RequestHeader("access_token") String token) {
    return token;
}

// 3. @RequestBody 请求多是 json 或 xml
@GetMapping("/v1/test5")
public String test5(@RequestBody User user) {
    return user.getUsername();
}

1.4 响应相关的注解

没有使用 @RestController 状况下返回 json 等格式须要使用 @ResponseBody 注解spring-boot

@RequestMapping("/v1/test")
@ResponseBody
public String test1() {
    return "test";
}

1.5 RequestEntity 和 ResponseEntity

@GetMapping("/v1/entity")
public ResponseEntity<String> test8() {
    return ResponseEntity.ok("hello, world!");
}

2、客户端 RestTemplate

常常须要发送一个 GET/POST 请求到其余系统(REST API),经过 JDK 自带的 HttpURLConnection、Apache HttpClient、Netty 四、OkHTTP 2/3 均可以实现。源码分析

HttpClient 的使用:http://rensanning.iteye.com/blog/1550436
RestTemplate 的使用:https://rensanning.iteye.com/blog/2362105post

Spring 的 RestTemplate 封装了这些库的实现,使用起来更简洁。ui

RestTemplate 包含如下几个部分:this

  1. HttpMessageConverter 对象转换器
  2. ClientHttpRequestFactory 默认是 JDK 的 HttpURLConnection
  3. ResponseErrorHandler 异常处理
  4. ClientHttpRequestInterceptor 请求拦截器

RestTemplate 结构

2.1 发送 GET 请求 getForObject()、getForEntity()、exchange()

// 1. getForObject()  
User user1 = this.restTemplate.getForObject(uri, User.class);  
  
// 2. getForEntity()  
ResponseEntity<User> responseEntity1 = this.restTemplate.getForEntity(uri, User.class);  
HttpStatus statusCode = responseEntity1.getStatusCode();  
HttpHeaders header = responseEntity1.getHeaders();  
User user2 = responseEntity1.getBody();  
  
// 3. exchange()  
RequestEntity requestEntity = RequestEntity.get(new URI(uri)).build();  
ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);  
User user3 = responseEntity2.getBody();

2.2 发送 POST 请求 postForObject()、postForEntity()、exchange()

// 1. postForObject()  
User user1 = this.restTemplate.postForObject(uri, user, User.class);  
  
// 2. postForEntity()  
ResponseEntity<User> responseEntity1 = this.restTemplate.postForEntity(uri, user, User.class);  
  
// 3. exchange()  
RequestEntity<User> requestEntity = RequestEntity.post(new URI(uri)).body(user);  
ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);

参考:

  1. 《REST访问(RestTemplate)》:https://rensanning.iteye.com/blog/2362105
  2. 《HTML 多媒体》:http://www.runoob.com/html/html-media.html

天天用心记录一点点。内容也许不重要,但习惯很重要!