springmvc @ResponseStatus和ResponseEntity的使用
使用@ResponseStatus和ResponseEntity
@ResponseStatus
是標(biāo)記一個方法或異常類在返回時響應(yīng)的http狀態(tài)。
其代碼注釋如下:
* * <p>The status code is applied to the HTTP response when the handler * method is invoked and overrides status information set by other means, * like {@code ResponseEntity} or {@code "redirect:"}. * * <p><strong>Warning</strong>: when using this annotation on an exception * class, or when setting the {@code reason} attribute of this annotation, * the {@code HttpServletResponse.sendError} method will be used. * * <p>With {@code HttpServletResponse.sendError}, the response is considered * complete and should not be written to any further. Furthermore, the Servlet * container will typically write an HTML error page therefore making the * use of a {@code reason} unsuitable for REST APIs. For such cases it is * preferable to use a {@link org.springframework.http.ResponseEntity} as * a return type and avoid the use of {@code @ResponseStatus} altogether. * * <p>Note that a controller class may also be annotated with * {@code @ResponseStatus} and is then inherited by all {@code @RequestMapping} * methods.
@ResponseStatus 可以結(jié)合 @ResponseBody 一起使用。
@RequestMapping("/testResponseBody") @ResponseBody public Map<String, String> testResponseBody(){ return ImmutableMap.of("key", "value"); } @RequestMapping("/testResponseBodyFaild") @ResponseBody @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public Map<String, String> testResponseBodyFaild(){ return ImmutableMap.of("key", "faild"); }
這兩個handler method返回的http status code(http狀態(tài)碼) 分別是200 和 501。
ResponseEntity
是在 org.springframework.http.HttpEntity 的基礎(chǔ)上添加了http status code(http狀態(tài)碼),用于RestTemplate以及@Controller的HandlerMethod。
它在Controoler中或者用于服務(wù)端響應(yīng)時,作用是和@ResponseStatus與@ResponseBody結(jié)合起來的功能一樣的。用于RestTemplate時,它是接收服務(wù)端返回的http status code 和 reason的。
代碼注釋如下:
* Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code. * Used in {@code RestTemplate} as well {@code @Controller} methods. * * <p>In {@code RestTemplate}, this class is returned by * {@link org.springframework.web.client.RestTemplate#getForEntity getForEntity()} and * {@link org.springframework.web.client.RestTemplate#exchange exchange()}: * <pre class="code"> * ResponseEntity<String> entity = template.getForEntity("http://example.com", String.class); * String body = entity.getBody(); * MediaType contentType = entity.getHeaders().getContentType(); * HttpStatus statusCode = entity.getStatusCode(); * </pre> * * <p>Can also be used in Spring MVC, as the return value from a @Controller method: * <pre class="code"> * @RequestMapping("/handle") * public ResponseEntity<String> handle() { * URI location = ...; * HttpHeaders responseHeaders = new HttpHeaders(); * responseHeaders.setLocation(location); * responseHeaders.set("MyResponseHeader", "MyValue"); * return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED); * } * </pre> * Or, by using a builder accessible via static methods: * <pre class="code"> * @RequestMapping("/handle") * public ResponseEntity<String> handle() { * URI location = ...; * return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World"); * } * </pre>
ResponseEntity 和 @ResponseStatus 一起使用是無效的
@RequestMapping("/testResponseEntity") public ResponseEntity<Map<String, String>> testResponseEntity(){ Map<String, String> map = ImmutableMap.of("key", "value"); return ResponseEntity.ok(map); } @RequestMapping("/testResponseEntityFaild") @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public ResponseEntity<Map<String, String>> testResponseEntityFaild(){ Map<String, String> map = ImmutableMap.of("key", "faild"); return ResponseEntity.ok(map); } @RequestMapping("/testResponseEntityFaild2") public ResponseEntity<Map<String, String>> testResponseEntityFaild2(){ return ResponseEntity.badRequest().body(ImmutableMap.of("key", "faild")); }
這三個handler method返回的http status code(http狀態(tài)碼) 分別是200 、 200 和 400。
完整的TestController代碼
import com.google.common.collect.ImmutableMap; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import java.util.Map; /** * Created by saleson on 2017/5/5. */ @RestController @RequestMapping("/test") public class TestController { @RequestMapping("/testResponseEntity") public ResponseEntity<Map<String, String>> testResponseEntity(){ Map<String, String> map = ImmutableMap.of("key", "value"); return ResponseEntity.ok(map); } @RequestMapping("/testResponseEntityFaild") @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public ResponseEntity<Map<String, String>> testResponseEntityFaild(){ Map<String, String> map = ImmutableMap.of("key", "faild"); return ResponseEntity.ok(map); } @RequestMapping("/testResponseEntityFaild2") public ResponseEntity<Map<String, String>> testResponseEntityFaild2(){ return ResponseEntity.badRequest().body(ImmutableMap.of("key", "faild")); } @RequestMapping("/testResponseBody") @ResponseBody public Map<String, String> testResponseBody(){ return ImmutableMap.of("key", "value"); } @RequestMapping("/testResponseBodyFaild") @ResponseBody @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) public Map<String, String> testResponseBodyFaild(){ return ImmutableMap.of("key", "faild"); } }
返回的http status code截圖:
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實現(xiàn)
本文主要介紹了SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04JAVA并發(fā)中VOLATILE關(guān)鍵字的神奇之處詳解
這篇文章主要給大家介紹了關(guān)于JAVA并發(fā)中VOLATILE關(guān)鍵字的神奇之處的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05MybatisPlus調(diào)用原生SQL的實現(xiàn)方法
本文主要介紹了MybatisPlus調(diào)用原生SQL的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02解決JSONObject.toJSONString()輸出null的問題
這篇文章主要介紹了解決JSONObject.toJSONString()輸出null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02詳解SpringBoot定制@ResponseBody注解返回的Json格式
這篇文章主要介紹了詳解SpringBoot定制@ResponseBody注解返回的Json格式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Springboot如何操作redis數(shù)據(jù)
這篇文章主要介紹了Springboot如何操作redis數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04