使用ResponseEntity作為的返回值的應用
ResponseEntity作為的返回值
通常如果后端想響應json數(shù)據(jù),我們需要在方法體上添加@Response注解,標注這個注解的方法的返回值會被spingmvc轉為json形式并寫入到響應體中。
但ResponseEntity則不會被springmvc轉換,可以使用這個類定義響應頭,狀態(tài)碼,響應體等。
@Controller public class ResponseBodyTest { @ResponseBody @GetMapping("b1") public R b1(){ //將方法的返回值轉為json寫入到響應體中 return R.ok().put("msg","success"); } @ResponseBody @GetMapping("b2") public ResponseEntity<String> b2(){ HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.TEXT_PLAIN); String str = "hello,world"; ResponseEntity responseEntity = new ResponseEntity<String>(str,httpHeaders,HttpStatus.OK); return responseEntity; } }
我們 /b2 請求設置了@ResponseBody注解,但由于返回的是ResponseEntity對象,而且我們又重新設置了響應類型為 text/plain,我們訪問b2請求查看結果:
心得
ResponseEntity的優(yōu)先級高于@ResponseBody。
在不是ResponseEntity的情況下才去檢查有沒有@ResponseBody注解。如果響應類型是ResponseEntity可以不寫@ResponseBody注解,寫了也沒有關系。
簡單的說@ResponseBody可以直接返回Json結果, @ResponseEntity不僅可以返回json結果,還可以定義返回的HttpHeaders和HttpStatus。
統(tǒng)一結果返回 ResponseEntity
在正規(guī)的嚴格的企業(yè)的前后端系統(tǒng)開發(fā)中,返回嚴謹?shù)臓顟B(tài)碼很有必要
平常大家為了統(tǒng)一格式返回,或許會自己封裝一個ResultUtils,然后自定義ResultCode枚舉類來返回,這樣有些麻煩;
我們可以使用SpringMVC為我們封裝的ResponseEntity對象來自定義狀態(tài)碼
源碼:
public class ResponseEntity<T> extends HttpEntity<T> { private final Object status; public ResponseEntity(HttpStatus status) { this((Object)null, (MultiValueMap)null, (HttpStatus)status); } public ResponseEntity(@Nullable T body, HttpStatus status) { this(body, (MultiValueMap)null, (HttpStatus)status); } public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) { this((Object)null, headers, (HttpStatus)status); } public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) { super(body, headers); Assert.notNull(status, "HttpStatus must not be null"); this.status = status; } private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) { super(body, headers); Assert.notNull(status, "HttpStatus must not be null"); this.status = status; }
@param body: the entity body
- ResponseEntity需要一個泛型T
- 代表我們需要傳入的數(shù)據(jù)對象
@param headers : the entity headers
- 我們可以new MultiValueMap<String, String> headers
- 設置響應頭信息
@param status: the status code
- 我們可以使用HttpStatus
- 也可以自己定義狀態(tài)碼(Object)
HttpStatus status是一個包含了各種響應狀態(tài)碼的枚舉類
// 201:創(chuàng)建成功 Created // 203 :沒有認證 NON_AUTHORITATIVE_INFORMATION // 204: 成功沒有返回值 No-content 一般是delete,update時使用 .....
例子:
@GetMapping("/categories") public ResponseEntity<List<Category>> getCategoryList(String token){ return new ResponseEntity<>(categoryService.queryAll(), HttpStatus.OK); } @PostMapping("/categories") public ResponseEntity<Category> AddCategory(String token, @RequestBody Category category){ return new ResponseEntity<>(categoryService.insert(category),HttpStatus.CREATED); }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java并發(fā)編程之Semaphore(信號量)詳解及實例
這篇文章主要介紹了Java并發(fā)編程之Semaphore(信號量)詳解及實例的相關資料,需要的朋友可以參考下2017-06-06SpringBoot啟動并初始化執(zhí)行sql腳本問題
這篇文章主要介紹了SpringBoot啟動并初始化執(zhí)行sql腳本問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01