RestTemplate 401 獲取錯誤信息的處理方案
RestTemplate 401錯誤
調(diào)用第三方api 若是服務返回狀態(tài)碼不為200,默認會執(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);
}
通常會直接已異常形勢拋出,若不特殊處理無法獲取返回提示信息。
需要捕捉HttpClientErrorException 異常,則可獲取返回信息
try{
......
}catch (HttpClientErrorException e) {
String resBody = e.getResponseBodyAsString();
log.info("客戶端異常返回:{}", resBody);
return new ResponseEntity<>(JSON.parseObject(resBody, res), e.getStatusCode());
}
一開始我這樣寫,死活返回的都是null
原來跟我設置的requestFactory有關
采用SimpleClientHttpRequestFactory 無法獲取提示
需要換成 HttpComponentsClientHttpRequestFactory
RestTemplate通過對象傳參,response的body為空討論
代碼復現(xiàn)
實體類
@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);
}
返回結果截圖:

返回結果為空的討論:返回的C類是jpa封裝后的類,即使通過json工具,也無法轉換成功
解決辦法一:實體類轉成普通類
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
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java使用異或運算實現(xiàn)簡單的加密解密算法實例代碼
這篇文章主要介紹了Java使用異或運算實現(xiàn)簡單的加密解密算法實例代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
使用@Value為靜態(tài)變量導入并使用導入的靜態(tài)變量進行初始化方式
這篇文章主要介紹了使用@Value為靜態(tài)變量導入并使用導入的靜態(tài)變量進行初始化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Spring?Boot+Aop記錄用戶操作日志實戰(zhàn)記錄
在Spring框架中使用AOP配合自定義注解可以方便的實現(xiàn)用戶操作的監(jiān)控,下面這篇文章主要給大家介紹了關于Spring?Boot+Aop記錄用戶操作日志實戰(zhàn)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
SpringBoot整合Redis實現(xiàn)刷票過濾功能
隨著互聯(lián)網(wǎng)的不斷發(fā)展,網(wǎng)站或APP的用戶流量增加,也衍生出了一些惡意刷量等問題,給數(shù)據(jù)分析及運營帶來極大的困難,所以本文使用SpringBoot和Redis實現(xiàn)一個刷票過濾功能,需要的可以參考一下2023-06-06

