RestTemplate 401 获取错误信息的处理方案
作者:xyw10000 时间:2023-01-14 10:37:44
RestTemplate 401错误
调用第三方api 若是服务返回状态码不为200,默认会执行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为空讨论
代码复现
实体类
@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
来源:https://blog.csdn.net/xuyw10000/article/details/88790391
标签:RestTemplate,401
0
投稿
猜你喜欢
Android Rreact Native 常见错误总结
2021-07-11 16:39:59
浅谈Spring-cloud 之 sleuth 服务链路跟踪
2022-09-24 16:07:43
c# 日历控件的实现
2022-08-21 11:02:22
全局记录Feign的请求和响应日志方式
2021-08-19 18:48:02
java图片缩放实现图片填充整个屏幕
2021-06-09 18:30:07
flutter实现底部导航栏
2023-08-23 01:06:13
UnityUI中绘制线状统计图
2022-12-03 14:30:43
Android项目实现短信的发送、接收和对短信进行拦截
2021-06-14 06:59:26
深入理解MyBatis中的一级缓存与二级缓存
2022-05-25 09:41:34
JavaMail与Spring整合过程解析
2022-07-06 23:53:54
java设计模式—静态代理模式(聚合与继承方式对比)
2022-08-08 05:35:58
Spring Boot中的那些条件判断的实现方法
2023-04-26 15:02:07
Unity Shader实现动态过场切换图片效果
2022-06-30 00:21:43
C#使用Tesseract进行Ocr识别的方法实现
2022-12-15 06:54:23
java编写的文件管理器代码分享
2023-11-20 13:28:52
Java栈之链式栈存储结构的实现代码
2022-01-18 09:38:48
利用C#守护Python进程的方法
2023-01-05 13:25:56
浅析Java SPI 与 dubbo SPI
2021-10-23 16:54:12
SpringMVC通过模型视图ModelAndView渲染视图的实现
2023-02-28 03:36:54
Android Handler机制详解原理
2023-10-29 22:03:44