整合SSJ(spring+springmvc+jpa)框架時遇到的No serializer問題
阿新 • • 發佈:2019-07-06
在類上加註解@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
第二種方案:
第一步:建立一個類
package com.zh.aisell.common; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; importcom.fasterxml.jackson.databind.SerializationFeature; 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); } }
第二步:在applicationContext-mvc.xml中配置,在支援springmvc註解中加上如下程式碼
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <propertyname="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.zh.aisell.common.CustomMapper"></bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>