采用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 cause

org.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<String> 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

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

springboot 采用HttpClient获取天气预报 异常及原因的更多相关文章

  1. HttpClient的CircularRedirectException异常原因及解决办法

    HttpClient的CircularRedirectException异常原因及解决办法 这两天在使用我自己爬虫抓取网页的时候总是出现 org.apache.http.client.ClientPr ...

  2. Android 采用HttpClient提交数据到服务器

    在前几篇文章中<Android 采用get方式提交数据到服务器><Android 采用post方式提交数据到服务器>介绍了android的两种提交数据到服务器的方法 本文继续介 ...

  3. 查看语句运行时间异常的原因(SQLServer)

    转载:http://www.cnblogs.com/fygh/archive/2012/01/17/2324926.html 查看语句运行时间异常的原因(SQLServer)   经常有开发同事反映如 ...

  4. SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy

    问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...

  5. SpringBoot 拦截器获取http请求参数

    SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...

  6. .net Monitor产生SynchronizationLockException异常的原因

    有时在使用Monitor进行并发同步编程时,会产生SynchronizationLockException异常,抛出的异常内容是"Object synchronization method ...

  7. 出现 org.springframework.beans.factory.BeanCreationException 异常的原因及解决方法

    1 异常描述 在从 SVN 检出项目并配置完成后,启动 Tomcat 服务器,报出如下错误: 2 异常原因 通过观察上图中被标记出来的异常信息,咱们可以知道 org.springframework.b ...

  8. jsp出现getOutputStream() has already been called for this response异常的原因和解决方法

    jsp出现getOutputStream() has already been called for this response异常的原因和解决方法 在tomcat5下jsp中出现此错误一般都是在js ...

  9. SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible

    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible ...

随机推荐

  1. tomcat下部署项目的流程和遇到的问题笔记

    简单部署流程: 1,解析域名关联到服务器ip 2,配置服务器jre运行环境 3,安装tomcat 4,项目打war包,放入tomcat根目录下webapps(tomcat默认加载的项目目录)目录下 5 ...

  2. Azure 本月最新活动,速度Mark!

    很多时候,为了知晓 Azure 相关活动的信息,需要到处查阅.问朋友同事,这样既麻烦又易造成延误.为方便广大粉丝,我们推出每月活动合集,帮您第一时间了解 Azure 最新活动,还等什么,一起来看吧! ...

  3. Jetty服务器的使用

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  4. 数组模拟栈(C语言)

    用数组模拟栈的实现: #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef struct ...

  5. mysqli:查询数据库中,是否存在数据的三种校验方法

    在我们编辑用户登录功能的时候,常常需要对用户输入的信息进行校验,校验的方法就是通过SQL语句进行一个比对,那么我们就需要用到以下三种中的一种进行校验啦 1.使用mysqli_num_rows()校验 ...

  6. failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg"

    https://stackoverflow.com/questions/43024337/why-this-error-when-i-try-to-create-workspaces-in-ros# ...

  7. 504. Inverted Index (Map Reduce) lintcode

    https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map redu ...

  8. Apache2.4 authz_core_module模块使用

    Description: Core Authorization Status: Base Moduledentifier: authz_core_module Sourceile: mod_authz ...

  9. 2017.11.14 C语言---指针的学习

    第八章 善于利用指针 (1)指针是什么 1.内存区每一个字节都有一个编号,这就是"地址".地址形象化的被称为"指针".它能通过以它为地址的内存单元.地址指向(* ...

  10. matlab中padarray函数在numpy、python中的实现

    a = np.arange(6) a = a.reshape((2, 3)) print np.lib.pad(a, 1, 'symmetric') 运行结果: [[ ] [ ] [ ] [ ]]