RestTemplate 401 獲取錯(cuò)誤信息的處理方案
RestTemplate 401錯(cuò)誤
調(diào)用第三方api 若是服務(wù)返回狀態(tài)碼不為200,默認(rèn)會(huì)執(zhí)行DefaultResponseErrorHandler
異常處理
@Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = getHttpStatusCode(response); switch (statusCode.series()) { case CLIENT_ERROR: throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
判斷是否異常
protected boolean hasError(HttpStatus statusCode) { return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR || statusCode.series() == HttpStatus.Series.SERVER_ERROR); }
通常會(huì)直接已異常形勢(shì)拋出,若不特殊處理無法獲取返回提示信息。
需要捕捉HttpClientErrorException 異常,則可獲取返回信息
try{ ...... }catch (HttpClientErrorException e) { String resBody = e.getResponseBodyAsString(); log.info("客戶端異常返回:{}", resBody); return new ResponseEntity<>(JSON.parseObject(resBody, res), e.getStatusCode()); }
一開始我這樣寫,死活返回的都是null
原來跟我設(shè)置的requestFactory有關(guān)
采用SimpleClientHttpRequestFactory 無法獲取提示
需要換成 HttpComponentsClientHttpRequestFactory
RestTemplate通過對(duì)象傳參,response的body為空討論
代碼復(fù)現(xiàn)
實(shí)體類
@Entity @Table(name = "a",schema = "a") @JsonIgnoreProperties(value = {"a"}) @Setter @Generated public class C { @Id @GeneratedValue private Integer id; @Column(name = "diseaseName",length = 255,nullable = false,unique = true) private String diseaseName; @Column(name = "description",length = 255,nullable = false,unique = true) private String description; @Column(name = "department",length = 255,nullable = false,unique = true) private String department; } controller @ResponseBody @RequestMapping(value = "",method = RequestMethod.POST) public Response APIcreate(@RequestBody C c) { String json = JSONUtil.toJSONString(c); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> entity = new HttpEntity<>(json, headers); String url = "http://localhost:3001/c"; ResponseEntity<Commondisease> responseEntity = restTemplate.postForEntity(url, entity, C.class); return new ResponseData(ExceptionMsg.SUCCESS, responseEntity); }
返回結(jié)果截圖:
返回結(jié)果為空的討論:返回的C類是jpa封裝后的類,即使通過json工具,也無法轉(zhuǎn)換成功
解決辦法一:實(shí)體類轉(zhuǎn)成普通類
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class C { private Integer id; private String diseaseName; private String description; private String department; } @ResponseBody @RequestMapping(value = "",method = RequestMethod.POST) public Response APIcreate(@RequestBody C c) { //C c = new Commondisease(1,"zhangsan","11","2222"); String json = JSONUtil.toJSONString(c); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> entity = new HttpEntity<>(json, headers); String url = "http://localhost:3001/c/"; ResponseEntity<Commondisease> responseEntity = restTemplate.postForEntity(url,entity,C.class); return new ResponseData(ExceptionMsg.SUCCESS,responseEntity); }
返回成功
解決辦法二:添加注解
@Data
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
把Jar文件轉(zhuǎn)成exe安裝文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄袹ar文件轉(zhuǎn)成exe安裝文件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Java使用異或運(yùn)算實(shí)現(xiàn)簡(jiǎn)單的加密解密算法實(shí)例代碼
這篇文章主要介紹了Java使用異或運(yùn)算實(shí)現(xiàn)簡(jiǎn)單的加密解密算法實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12使用@Value為靜態(tài)變量導(dǎo)入并使用導(dǎo)入的靜態(tài)變量進(jìn)行初始化方式
這篇文章主要介紹了使用@Value為靜態(tài)變量導(dǎo)入并使用導(dǎo)入的靜態(tài)變量進(jìn)行初始化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)記錄
在Spring框架中使用AOP配合自定義注解可以方便的實(shí)現(xiàn)用戶操作的監(jiān)控,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Spring 注解編程模型相關(guān)知識(shí)詳解
這篇文章主要介紹了Spring 注解編程模型相關(guān)知識(shí)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09SpringBoot整合Redis實(shí)現(xiàn)刷票過濾功能
隨著互聯(lián)網(wǎng)的不斷發(fā)展,網(wǎng)站或APP的用戶流量增加,也衍生出了一些惡意刷量等問題,給數(shù)據(jù)分析及運(yùn)營(yíng)帶來極大的困難,所以本文使用SpringBoot和Redis實(shí)現(xiàn)一個(gè)刷票過濾功能,需要的可以參考一下2023-06-06