HttpMessageNotReadableException 异常解决

这种异常通常是jackson反序列化失败引起,例如

InvalidFormatException: Can not deserialize value of type java.util.Date

 

获取天气信息

RestTemplate template = new RestTemplate();
Weather weather = template.getForObject("https://www.tianqiapi.com/api?version=v1&ip={ip}", Weather.class, IPUtil.getIpAddr(request));
            

需要在Weather的属性update_time加入json解析注解

public class Weather {

    private String cityid;

    @com.fasterxml.jackson.annotation.JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date update_time;

    ...
    ...
}

 

 HttpMessageNotReadableException 异常解决_第1张图片

你可能感兴趣的:(Java,原创)