亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

如何使用spring ResponseEntity處理http響應

瀏覽:4日期:2023-06-28 09:29:49
簡介

使用spring時,達到同一目的通常有很多方法,對處理http響應也是一樣。本文我們學習如何通過ResponseEntity設置http相應內容、狀態以及頭信息。

ResponseEntity

ResponseEntity標識整個http相應:狀態碼、頭部信息以及相應體內容。因此我們可以使用其對http響應實現完整配置。

如果需要使用ResponseEntity,必須在請求點返回,通常在spring rest中實現。ResponseEntity是通用類型,因此可以使用任意類型作為響應體:

@GetMapping('/hello')ResponseEntity<String> hello() { return new ResponseEntity<>('Hello World!', HttpStatus.OK);}

可以通過編程方式指明響應狀態,所以根據不同場景返回不同狀態:

@GetMapping('/age')ResponseEntity<String> age( @RequestParam('yearOfBirth') int yearOfBirth) { if (isInFuture(yearOfBirth)) {return new ResponseEntity<>( 'Year of birth cannot be in the future', HttpStatus.BAD_REQUEST); } return new ResponseEntity<>( 'Your age is ' + calculateAge(yearOfBirth), HttpStatus.OK);}

另外,還可以設置http響應頭:

@GetMapping('/customHeader')ResponseEntity<String> customHeader() { HttpHeaders headers = new HttpHeaders(); headers.add('Custom-Header', 'foo'); return new ResponseEntity<>( 'Custom header set', headers, HttpStatus.OK);}

而且, ResponseEntity提供了兩個內嵌的構建器接口: HeadersBuilder 和其子接口 BodyBuilder。因此我們能通過ResponseEntity的靜態方法直接訪問。

最簡單的情況是相應包括一個主體及http 200響應碼:

@GetMapping('/hello')ResponseEntity<String> hello() { return ResponseEntity.ok('Hello World!');}

大多數常用的http 響應碼,可以通過下面static方法:

BodyBuilder accepted();BodyBuilder badRequest();BodyBuilder created(java.net.URI location);HeadersBuilder<?> noContent();HeadersBuilder<?> notFound();BodyBuilder ok();

另外,可以能使用BodyBuilder status(HttpStatus status)和BodyBuilder status(int status) 方法設置http狀態。使用ResponseEntity BodyBuilder.body(T body)設置http響應體:

@GetMapping('/age')ResponseEntity<String> age(@RequestParam('yearOfBirth') int yearOfBirth) { if (isInFuture(yearOfBirth)) {return ResponseEntity.badRequest() .body('Year of birth cannot be in the future'); } return ResponseEntity.status(HttpStatus.OK).body('Your age is ' + calculateAge(yearOfBirth));}

也可以自定義頭信息:

@GetMapping('/customHeader')ResponseEntity<String> customHeader() { return ResponseEntity.ok().header('Custom-Header', 'foo').body('Custom header set');}

因為BodyBuilder.body()返回ResponseEntity 而不是 BodyBuilder,需要最后調用。注意使用HeaderBuilder 不能設置任何響應體屬性。

盡管ResponseEntity非常強大,但不應該過度使用。在一些簡單情況下,還有其他方法能滿足我們的需求,使代碼更整潔。

替代方法@ResponseBody

典型spring mvc應用,請求點通常返回html頁面。有時我們僅需要實際數據,如使用ajax請求。這時我們能通過@ResponseBody注解標記請求處理方法,審批人能夠處理方法結果值作為http響應體。

@ResponseStatus

當請求點成功返回,spring提供http 200(ok)相應。如果請求點拋出異常,spring查找異常處理器,由其返回相應的http狀態碼。對這些方法增加@ResponseStatus注解,spring會返回自定義http狀態碼。

直接操作相應

Spring 也允許我們直接 javax.servlet.http.HttpServletResponse 對象;只需要申明其作為方法參數:

@GetMapping('/manual')void manual(HttpServletResponse response) throws IOException { response.setHeader('Custom-Header', 'foo'); response.setStatus(200); response.getWriter().println('Hello World!');}

但需要說明,既然spring已經提供底層實現的抽象和附件功能,我們不建議直接操作response。

總結:本文我們介紹了spring提供多種方式處理http響應,以及各自的優缺點,希望對你有幫助。

ResponseEntity的基本簡介1、ResponseEntity繼承了HttpEntity

可以添加HttpStatus狀態碼的HttpEntity的擴展類。被用于RestTemplate和Controller層方法

2、ResponseEntity可以定義返回的HttpStatus(狀態碼)

和HttpHeaders(消息頭:請求頭和響應頭)HttpStatus的狀態碼有以下幾種

如何使用spring ResponseEntity處理http響應

3、ResponseEntity的優先級高于@ResponseBody

在不是ResponseEntity的情況下才去檢查有沒有@ResponseBody注解。如果響應類型是ResponseEntity可以不寫@ResponseBody注解,寫了也沒有關系。

簡單的說@ResponseBody可以直接返回Json結果,@ResponseEntity不僅可以返回json結果,還可以定義返回的HttpHeaders和HttpStatus

public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = 'pid',defaultValue = '0') Long pid){if(pid == null || pid.longValue()<0){ // 響應400,相當于ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); return ResponseEntity.badRequest().build();}//ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // ResponseEntity.notFound().build();// ResponseEntity.ok(null);List<Category> categoryList = this.categoryService.queryCategoriesByPid(pid);if(CollectionUtils.isEmpty(categoryList)){ // 響應404 return ResponseEntity.notFound().build();}return ResponseEntity.ok(categoryList); }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 激情五月色综合色婷婷 | 91精品国产手机在线版 | 欧美一级毛片做受 | 国产精品人成福利视频 | 一个色综合网 | 国产系列 视频二区 | 国产精品嫩草影院视频 | 亚洲色图第一页 | 波多野结中文字幕在线69视频 | 小明福利 | 国产精品久久久久毛片真精品 | 午夜撸| 在线成人观看 | 美女喷水视频在线观看 | 91视频 - 88av| 免费碰碰碰视频在线看 | 伊人婷婷色 | 最新国产网站 | 亚洲精品视频在线观看视频 | 久久久男女野外野战 | 中国一级做a爱片免费 | 真实偷清晰对白在线视频 | 亚洲国产精品美女 | 国产黄色大全 | 亚洲啪啪免费视频 | 欧美日韩中文字幕 | 成年网站在线观看视频 | 欧美色图一区二区 | maomiav在线| 青青草91 | 日本二区免费一片黄2019 | 在线观看视频黄色 | 日韩小视频 | 亚洲婷婷国产精品电影人久久 | 亚洲欧美一区二区三区四区 | 黄色自拍视频 | 精品三级国产一区二区三区四区 | 正在播放淫亚洲 | 精品欧美一区二区三区在线观看 | 亚洲精品国产精品国自产 | 麻豆视频在线播放 |