restTemplate解析401异常失败

系列文章目录

第一章 spring框架构成
第二章 spring容器
第三章 spring配置bean
第四章 bean的继承和依赖
第五章 bean的生命周期
第六章 spring依赖注入
第七章 Spring AOP
第八章 spring事务
第九章 spring事件监听
第十章 web应用使用spring
第十一章 spring注解整理
第十二章 spring事务失效场景
第十三章 spring bean的作用域
第十四章 spring的扩展接口
第十五章 spring引入外部属性文件
第十六章 spring自定义属性编辑器
第十七章 spring配置双数据源
第十八章 restTemplate解析401异常失败



restTemplate解析401异常失败

原文链接 https://zhhll.icu/2020/框架/spring/restTemplate/restTemplate解析401异常失败/

如果使用restTemplate默认构造器的话,在进行错误解析的时候,使用

if (response.getStatusCode().isError()) { // 4XX或者5XX
		InputStream inputStream = response.getBody();
}

// 此时这个response是SimpleClientHttpResponse
// 此方法为SimpleClientHttpResponse的getBody()方法
@Override
	public InputStream getBody() throws IOException {
        // 在这一行取出的errorStream是null  这个connection使用的是private final HttpURLConnection connection;
		InputStream errorStream = this.connection.getErrorStream();
		this.responseStream = (errorStream != null ? errorStream : this.connection.getInputStream());
		return this.responseStream;
}

这时候需要在创建restTemplate的时候更改requestFactory,使用HttpComponentsClientHttpRequestFactory

RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值