博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 采用HttpClient获取天气预报 异常及原因
阅读量:4313 次
发布时间:2019-06-06

本文共 2042 字,大约阅读时间需要 6 分钟。

采用httpClient调用天气预报地址获取出现异常

2018-10-04 15:18:25.815 ERROR 10868 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException: 502 Bad Gateway] with root causeorg.springframework.web.client.HttpServerErrorException: 502 Bad Gateway    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RE LEASE]

当前异常产生原因是调用服务器地址我写错了导致的异常

错误代码:

正确的服务地址:

第二个异常json转换异常

 正确获取到一个json格式数据后要将当前json字符串转换成指定类型对象

private WeatherResponse doGetWeather(String uri){        //通过spring  restTemplate 客户端发送请求获取 String类型响应实体        ResponseEntity
respString = restTemplate.getForEntity(uri, String.class); //Springboot内部集成了json操作对象 ObjectMapper objectMapper = new ObjectMapper(); WeatherResponse resp=null; String strBody = null; //通过响应客户端获取状态码判断是否为成功状态码 if(respString.getStatusCodeValue()==200){ //获取响应实体内容实体 strBody = respString.getBody(); } try { //通过json操作对象将string 类型数据转为指定类型对象 resp = objectMapper.readValue(strBody, WeatherResponse.class); } catch (IOException e) { e.printStackTrace(); } return resp; }}

由于weatherResponse实体内定义的属性与json 返回属性不一致

public class WeatherResponse implements Serializable{    private Weather weather;    private Integer status;    private String desc;

json数据属性:  返回json 数据属性为data  而实体中属性Weather 为weather 应该修改为Weather  data   

异常信息:这里会清晰的指出属性是哪一个问题

 

转载于:https://www.cnblogs.com/lwdmaib/p/9742347.html

你可能感兴趣的文章
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>