RestTemplate 非200请求 响应获取的方法
方法一:
响应是非200请求会抛异常,可以用try去捕获
1 2 3 4 5 6 7 8 9 10 11 12 | try { sinGnUpOne_Method(req,token,skuurl); // 获取200响应 // {"id":6,"path":"/free/xxxx","reserve":0} System. out .println( "看下抛异常走不走" ); } catch (Exception e) { System. out .println(e.getCause()); // 获取非200的响应 // 400 : [{"error_code":"22222@您已存在订单"}] System. out .println(e.getMessage()); ----拿到的是String类型 System. out .println( "走catch" ); |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 | try { sinGnUpOne_Method(req,token,skuurl); // 获取200响应 // {"id":6,"path":"/free/xxxx","reserve":0} System. out .println( "看下抛异常走不走" ); } catch (HttpClientErrorException e) { String resBody = e.getResponseBodyAsString(); String code = e.getStatusCode().toString(); //{"error_code":"22222@您已存在订单"} System. out .println(resBody); //400 BAD_REQUEST System. out .println(code); } |
方法三:
实现ResponseErrorHandler重写,非200也正常返回
实现自定义RestTemplateResponseErrorHandler
RestTemplateResponseErrorHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.Common.restTemplate; import com.sun.jersey.api.NotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.ResponseErrorHandler; import java.io.IOException; @Component public class RestTemplateResponseErrorHandler implements ResponseErrorHandler { @Override public boolean hasError(ClientHttpResponse httpResponse)throws IOException { return (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR || httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR); } @Override public void handleError(ClientHttpResponse httpResponse)throws IOException { if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) { throw new HttpClientErrorException(httpResponse.getStatusCode()); } else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) { if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) { throw new NotFoundException(); } } } } |
将ResponseErrorHandler实现注入到RestTemplate实例中
RequestConfig --配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.config; import com.Common.restTemplate.RestTemplateResponseErrorHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RequestConfig{ @Bean public RestTemplate restTemplate(RestTemplateResponseErrorHandler restTemplateResponseErrorHandler) throws Exception { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(restTemplateResponseErrorHandler); return restTemplate; } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); //读取超时时间设置 factory.setReadTimeout(5000); //连接超时时间设置 factory.setConnectTimeout(15000); return factory; } } |
参考:
https://blog.csdn.net/y534560449/article/details/115658680?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_v2_default&depth_1-utm_source=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_v2_default
方法三的文件放置
发送请求相关见 https://www.cnblogs.com/kaibindirver/p/15398815.html
分类:
SpingBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2020-03-01 查企业 和 失信人 网站,还有官师案例
2019-03-01 优化 要引入多个 模块 使用调用的方法,让管理更便捷 --execfile() 函数
2018-03-01 post参数的方法 json data 和特别的传参
2018-03-01 接口测试简介
2018-03-01 appium的三种等待方式 (还没实践过,记录在此)