亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springmvc @ResponseStatus和ResponseEntity的使用

 更新時間:2024年07月04日 15:51:02   作者:Saleson  
這篇文章主要介紹了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&lt;String&gt; 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">
 * &#64;RequestMapping("/handle")
 * public ResponseEntity&lt;String&gt; handle() {
 *   URI location = ...;
 *   HttpHeaders responseHeaders = new HttpHeaders();
 *   responseHeaders.setLocation(location);
 *   responseHeaders.set("MyResponseHeader", "MyValue");
 *   return new ResponseEntity&lt;String&gt;("Hello World", responseHeaders, HttpStatus.CREATED);
 * }
 * </pre>
 * Or, by using a builder accessible via static methods:
 * <pre class="code">
 * &#64;RequestMapping("/handle")
 * public ResponseEntity&lt;String&gt; 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)

    本文主要介紹了SpringBoot、mybatis返回樹結(jié)構(gòu)的數(shù)據(jù)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • JAVA并發(fā)中VOLATILE關(guān)鍵字的神奇之處詳解

    JAVA并發(fā)中VOLATILE關(guān)鍵字的神奇之處詳解

    這篇文章主要給大家介紹了關(guān)于JAVA并發(fā)中VOLATILE關(guān)鍵字的神奇之處的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Java異常處理中的一些特殊情況舉例

    Java異常處理中的一些特殊情況舉例

    這篇文章主要介紹了Java異常處理中的一些特殊情況舉例,分別是只用try和finally不用catch,以及finally語句不被執(zhí)行的情況,需要的朋友可以參考下
    2015-11-11
  • MybatisPlus調(diào)用原生SQL的實現(xiàn)方法

    MybatisPlus調(diào)用原生SQL的實現(xiàn)方法

    本文主要介紹了MybatisPlus調(diào)用原生SQL的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java這個名字的來歷與優(yōu)勢

    Java這個名字的來歷與優(yōu)勢

    Java是Sun公司開發(fā)的一種編程語言,Sun公司最初的方向是讓Java來開發(fā)一些電器裝置程序,Java名字的由來,實際上是一個有趣的故事。
    2014-10-10
  • 解決JSONObject.toJSONString()輸出null的問題

    解決JSONObject.toJSONString()輸出null的問題

    這篇文章主要介紹了解決JSONObject.toJSONString()輸出null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 詳解SpringBoot定制@ResponseBody注解返回的Json格式

    詳解SpringBoot定制@ResponseBody注解返回的Json格式

    這篇文章主要介紹了詳解SpringBoot定制@ResponseBody注解返回的Json格式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Springboot如何操作redis數(shù)據(jù)

    Springboot如何操作redis數(shù)據(jù)

    這篇文章主要介紹了Springboot如何操作redis數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Java定時器Timer簡述

    Java定時器Timer簡述

    本文主要介紹了Java定時器Timer的相關(guān)知識,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Java項目中防止SQL注入的四種方法推薦

    Java項目中防止SQL注入的四種方法推薦

    sql注入是web開發(fā)中最常見的一種安全漏洞,這篇文章為大家整理了四種Java項目中防止SQL注入的方法,有需要的小伙伴可以參考一下
    2025-03-03

最新評論