后台controller返回前台json HttpMessageNotWritableException报错的解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuxiao_wei/article/details/83986783

报错信息如下:

2018-11-12 09:25:14.644  WARN 9416 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
 

分析:报错的类==>org.springframework.http.converter.HttpMessageNotWritableException

原因:Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS);

重点:FAIL_ON_EMPTY_BEANS

那么,显然,json 序列化失败,失败的原因是 返回的类属性里有空值,在数据里查数据发现

确实有空值 则 ,在实体类中加入注解

@JsonSerialize(include=Inclusion.NON_EMPTY) 

当然这个注解可以作为全局注解 放在类上,也可以作为局部注解,放在某个具体字段上 

这个注解似乎失效了,但还能用。到此结束。

猜你喜欢

转载自blog.csdn.net/wuxiao_wei/article/details/83986783