Spring 系列目錄(http://www.javashuo.com/article/p-hskusway-em.html)html
SpringBoot REST 系列相關的文章:java
Spring 有如下相關的註解:spring
## 定義相關 |-- @Controller |-- @RestController ## 映射相關 |-- @RequestMapping |-- @GetMapping |-- @PostMapping |-- @PathVariable ## 請求相關 |-- @RequestBody |-- @RequestParam |-- @RequestHeader |-- @CookieValue |-- RequestEntity ## 定義相關 |-- @ResponseBody |-- ResponseEntity
@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"; } }
// @PathVariable 解析請求路徑中的參數 @RequestMapping("/v1/{city_id}/{user_id}") public String test2(@PathVariable("city_id") String cityId, @PathVariable(value = "user_id") String userId) { return cityId; }
// 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(); }
沒有使用 @RestController 狀況下返回 json 等格式須要使用 @ResponseBody 註解spring-boot
@RequestMapping("/v1/test") @ResponseBody public String test1() { return "test"; }
@GetMapping("/v1/entity") public ResponseEntity<String> test8() { return ResponseEntity.ok("hello, world!"); }
常常須要發送一個 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
HttpMessageConverter
對象轉換器ClientHttpRequestFactory
默認是 JDK 的 HttpURLConnectionResponseErrorHandler
異常處理ClientHttpRequestInterceptor
請求攔截器// 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();
// 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);
參考:
天天用心記錄一點點。內容也許不重要,但習慣很重要!