最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springmvc @ResponseStatus和ResponseEntity的使用

 更新時(shí)間:2024年07月04日 15:51:02   作者:Saleson  
這篇文章主要介紹了springmvc @ResponseStatus和ResponseEntity的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用@ResponseStatus和ResponseEntity

@ResponseStatus

是標(biāo)記一個(gè)方法或異常類在返回時(shí)響應(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");
}

這兩個(gè)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)時(shí),作用是和@ResponseStatus與@ResponseBody結(jié)合起來的功能一樣的。用于RestTemplate時(shí),它是接收服務(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"));
}

這三個(gè)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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過實(shí)例解析spring bean之間的關(guān)系

    通過實(shí)例解析spring bean之間的關(guān)系

    這篇文章主要介紹了通過實(shí)例解析spring bean之間的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • java操作文件之文件重命名實(shí)現(xiàn)方式

    java操作文件之文件重命名實(shí)現(xiàn)方式

    這篇文章主要介紹了java操作文件之文件重命名實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName

    RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName

    這篇文章主要介紹了RocketMq同組消費(fèi)者如何自動(dòng)設(shè)置InstanceName問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄流程圖解

    基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄流程圖解

    這篇文章主要介紹了基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄流程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot配置圖片訪問的虛擬路徑

    SpringBoot配置圖片訪問的虛擬路徑

    大家好,本篇文章主要講的是SpringBoot配置圖片訪問的虛擬路徑,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • javaWeb使用servlet搭建服務(wù)器入門

    javaWeb使用servlet搭建服務(wù)器入門

    這篇文章主要為大家詳細(xì)介紹了javaWeb使用servlet搭建服務(wù)器入門,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • @Bean注解和@Configuration、@Component注解組合使用的區(qū)別

    @Bean注解和@Configuration、@Component注解組合使用的區(qū)別

    這篇文章主要介紹了@Bean注解和@Configuration、@Component注解組合使用的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java根據(jù)負(fù)載自動(dòng)抓取jstack?dump詳情

    java根據(jù)負(fù)載自動(dòng)抓取jstack?dump詳情

    這篇文章主要介紹了java根據(jù)負(fù)載自動(dòng)抓取jstack?dump詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • java并發(fā)訪問重復(fù)請(qǐng)求過濾問題

    java并發(fā)訪問重復(fù)請(qǐng)求過濾問題

    本篇文章給大家分享了關(guān)于java并發(fā)訪問重復(fù)請(qǐng)求過濾的相關(guān)問題以及解決方法,對(duì)此有需要的朋友參考學(xué)習(xí)下。
    2018-05-05
  • Java的設(shè)計(jì)模式之代理模式使用詳解

    Java的設(shè)計(jì)模式之代理模式使用詳解

    這篇文章主要介紹了Java的設(shè)計(jì)模式之代理模式使用詳解,代理模式是23種設(shè)計(jì)模式之一,它關(guān)心的主要是過程,而不是結(jié)果,代理模式主要提供了對(duì)目標(biāo)對(duì)象的間接訪問方式,即通過代理對(duì)象來訪問目標(biāo)對(duì)象,需要的朋友可以參考下
    2024-01-01

最新評(píng)論

青冈县| 马山县| 秦安县| 阳城县| 崇文区| 独山县| 天柱县| 宝清县| 长春市| 江北区| 沙雅县| 仲巴县| 迭部县| 建宁县| 金阳县| 美姑县| 涟源市| 交口县| 长汀县| 仲巴县| 溧水县| 克山县| 开原市| 西畴县| 正蓝旗| 馆陶县| 怀安县| 湖南省| 汾西县| 合作市| 徐水县| 兰考县| 吉木乃县| 铜陵市| 古田县| 大悟县| 剑阁县| 浦江县| 永济市| 福鼎市| 石河子市|