Spring MVC解決Could not write JSON:No serializer found for class java.lang.Object
10466 ワード
Spring MVC解決Could not write JSON:No serializer found for class java.lang.Object
資料の参考:http://stackoverflow.com/questions/28862483/spring-and-jackson-how-to-disable-fail-on-empty-beans-through-responsebody
問題が発生する前の配置
SpringMVCのコメント@ResonseBodyは以下の場合に使用します。
ソリューション
SpringMVCにデフォルトのプログレッシブに使用されるcomp.fasterxml.jackson.databind.Object MapperのプロパティSerialization Feature.FAIL_を設定します。ONするEMPTY_BEANSはfalseである直接XML設定には設定できないようです。comp.faster xml.jackson.databind.Object Mapper類のみをカスタマイズして設定し、xmlを再配置することができます。プロセスは以下の通りです
属性Serialzation Feature.FAIL_についてONするEMPTY_BEANSの詳細は本文の範囲内ではなく、Jackson文書とソースコードをご参照ください。
資料の参考:http://stackoverflow.com/questions/28862483/spring-and-jackson-how-to-disable-fail-on-empty-beans-through-responsebody
問題が発生する前の配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8value>
<value>text/html;charset=UTF-8value>
list>
property>
bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8value>
<value>application/x-www-form-urlencoded; charset=UTF-8value>
list>
property>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULLvalue>
property>
bean>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
問題の説明SpringMVCのコメント@ResonseBodyは以下の場合に使用します。
@ResponseBody
@RequestMapping("/your_url/act.do")
public Map<String, Object> yourMethod(){
// do your work...
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("code", 0);
resultMap.put("data", new Object());// new Obejct()
}
次のエラーを報告します DefaultHandlerExceptionResolver:134 : Resolving exception from handler []: org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) ;
Objectに使えるプログレッシブが見つからず、属性も見つけられずビーンSerializerを作成するという意味です。その後、解決方法を提示しました。ソリューション
SpringMVCにデフォルトのプログレッシブに使用されるcomp.fasterxml.jackson.databind.Object MapperのプロパティSerialization Feature.FAIL_を設定します。ONするEMPTY_BEANSはfalseである直接XML設定には設定できないようです。comp.faster xml.jackson.databind.Object Mapper類のみをカスタマイズして設定し、xmlを再配置することができます。プロセスは以下の通りです
// ObjectMapper
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);
}
}
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8value>
<value>text/html;charset=UTF-8value>
list>
property>
bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8value>
<value>application/x-www-form-urlencoded; charset=UTF-8value>
list>
property>
<property name="objectMapper">
<bean class="com.king.framework.jackson.CustomMapper">
bean>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
付属する属性Serialzation Feature.FAIL_についてONするEMPTY_BEANSの詳細は本文の範囲内ではなく、Jackson文書とソースコードをご参照ください。