緣由:hibernate添加了一些字段(使變成Json時報錯)java
解決方案一: 字段上加註解 spring
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
解決方案二:本身配置(拷備過來用便可)json
1. 新建一個CustomMapper 類mvc
public class CustomMapper extends ObjectMapper { public CustomMapper() { this.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 設置 SerializationFeature.FAIL_ON_EMPTY_BEANS 爲 false this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); } }
2.在applicationContext-mvc.xml配置app
<!-- Spring MVC 配置 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json; charset=UTF-8</value> <value>application/x-www-form-urlencoded; charset=UTF-8</value> </list> </property> <!-- No serializer:配置 objectMapper 爲咱們自定義擴展後的 CustomMapper,解決了返回對象有關係對象的報錯問題 --> <property name="objectMapper"> <--本身類的全限定名--> <bean class="com.yilin.aisell.common.CustomMapper"></bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>