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)文章
SpringBoot2.X整合Spring-Cache緩存開發(fā)的實(shí)現(xiàn)
本文主要介紹了SpringBoot2.X整合Spring-Cache緩存開發(fā)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架)
這篇文章主要介紹了關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Java異常處理之try...catch...語句的使用進(jìn)階
這篇文章主要介紹了Java異常處理之try...catch...語句的使用進(jìn)階,重點(diǎn)在于牽扯相關(guān)IO使用時(shí)的資源調(diào)配情況,需要的朋友可以參考下2015-11-11
@valid 無法觸發(fā)BindingResult的解決
這篇文章主要介紹了@valid 無法觸發(fā)BindingResult的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
spring-cloud入門之eureka-server(服務(wù)發(fā)現(xiàn))
本篇文章主要介紹了spring-cloud入門之eureka-server(服務(wù)發(fā)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Java中循環(huán)冗余校驗(yàn)(CRC32)的實(shí)現(xiàn)
CRC校驗(yàn)實(shí)用程序庫在數(shù)據(jù)存儲(chǔ)和數(shù)據(jù)通訊領(lǐng)域,為了保證數(shù)據(jù)的正確,就不得不采用檢錯(cuò)的手段,下面這篇文章主要給大家介紹了關(guān)于Java中循環(huán)冗余校驗(yàn)(CRC32)實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
spring boot2結(jié)合mybatis增刪改查的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于spring boot2結(jié)合mybatis增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

