不管是單體應用還是微服務應用, 現在都流行Restful風格, 下圖是一個比較典型的使用rest的應用架構, 該應用不僅使用database數據源, 而且用到了一個Weather微服務, 另一方面, 該應用也是通過rest方式為web UI 或其他微服務應用提供服務.
=============================
通過Postman 插件測試Rest接口
=============================
之前使用postman 插件調試rest接口總報 415 Unsupported Media Type錯誤, 原因是: HEADERS中必須設置Content-type為application/json, 后台才能順利接收到參數. 見下圖截圖.
{
"timestamp": "2018-09-07T06:49:57.620+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/books"
}
=============================
與 Rest 服務交互的幾個環節
=============================
一. Spring 后台與其他Rest服務的數據交互
Spring 提供了 RestTemplate 類, 方便和其他Rest服務交互數據
二. 在web response中 返回 json 數據
對於Restful的視圖方法, 其返回類型可以是 ResponseEntity<T> 或者 ResponseEntity<List<T>>, Spring會自動將返回值轉換為json格式. 如果要輸出多個對象的復合體json, 可以在java中定義一個Wrapper類組合多個Pojo類, 然后返回這個Wrapper對象即可.
三. 接收 web Request 中的 json 數據
視圖方法的 @RequestBody 注釋參數可以是Map類型, 也可以是Pojo類型. 如果是Pojo類型, Spring就自動完成json->object的轉換, 如果是map類型, Spring就自動完成kv映射. 如果json數據是多個object的某種組合, 我們可以在java中定義一個Wrapper類組合多個Pojo類, 專門接收 Request 中的json數據.
=============================
示例代碼
=============================
Pojo代碼:
class Office { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } class Car { private String brand; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } } /* * Boss 類是 Office 和 Office 類的組合 */ class Boss { private Car car; private Office office; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Office getOffice() { return office; } public void setOffice(Office office) { this.office = office; } }
Controller和主函數代碼:
@SpringBootApplication @RestController public class Demo1Application { // 示例: 一個對象的json response // 測試url為 http://localhost:8080/oneOffice // 結果為 {"address":"address1"} @ResponseBody @RequestMapping(value = "/oneOffice", method = RequestMethod.GET) public ResponseEntity<Office> oneOffice() { Office office = new Office(); office.setAddress("address1"); return new ResponseEntity<Office>(office, HttpStatus.OK); } // 示例: 一個集合的json response // 測試url為 http://localhost:8080/offices // 結果為 [{"address":"address1"},{"address":"address2"}] @ResponseBody @RequestMapping(value = "/offices", method = RequestMethod.GET) public ResponseEntity<List<Office>> offices() { List<Office> offices = new ArrayList<Office>(); Office office1 = new Office(); office1.setAddress("address1"); Office office2 = new Office(); office2.setAddress("address2"); offices.add(office1); offices.add(office2); return new ResponseEntity<List<Office>>(offices, HttpStatus.OK); } // 示例: 一個簡單對象的 Post 示例 // 測試url為 http://localhost:8080/offices // 提交的data為 { "address": "address1" } @RequestMapping(value = "/offices", method = RequestMethod.POST, consumes = "application/json") @ResponseBody public String createOffice(@RequestBody Office office) { if (office != null && office.getAddress() != null) { return "OK"; } else { return "Empty"; } } // 示例: 一個組合對象的 response 示例 // 測試url為 http://localhost:8080/oneBoss // 結果為 {"car":null,"office":{"address":"address1"}} @RequestMapping(value = "/oneBoss", method = RequestMethod.GET) @ResponseBody public ResponseEntity<Boss> oneBoss() { Office office = new Office(); office.setAddress("address1"); Boss objectWrapper = new Boss(); objectWrapper.setOffice(office); return new ResponseEntity<Boss>(objectWrapper, HttpStatus.OK); } // 示例: 一個組合對象的 Post 示例 // 測試url為 http://localhost:8080/bosses // 提交的data為 {"car":null,"office":{"address":"address1"}} @RequestMapping(value = "/bosses", method = RequestMethod.POST, consumes = "application/json") @ResponseBody public String createBoss(@RequestBody Boss boss) { if (boss != null && boss.getOffice() != null && boss.getOffice().getAddress() != null) { return "OK"; } else { return "Empty"; } } public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }
=========================
參考
=========================
https://www.leveluplunch.com/java/tutorials/014-post-json-to-spring-rest-webservice/
https://javabeat.net/rest-api-best-practices/
https://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration
截圖來自: https://github.com/hamvocke/testing-microservices-ebook/blob/master/testing-microservices.adoc