HttpMessageNotReadableException 异常解决

  • Post author:
  • Post category:其他

这种异常通常是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;

    ...
    ...
}

 

 


版权声明:本文为tiankongzhichenglyf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。