Spring MVC 解決 Could not write JSON: No serializer found for class java.lang.Object


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

出問題前的配置

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <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>
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="serializationInclusion">
                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
     
     
    
   
  
  
          

問題描述

SpringMVC的注解@ResponseBody在以下情況使用時:

@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的序列化器,也沒有找到屬性去創建BeanSerializer。
后面的接着提示了解決方法:(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

解決方案

為SpringMVC里默認序列化使用的 com.fasterxml.jackson.databind.ObjectMapper 設置其屬性 SerializationFeature.FAIL_ON_EMPTY_BEANS 為false。
貌似沒辦法直接在XML配置里設置。只能自定義 com.fasterxml.jackson.databind.ObjectMapper 類進行設置,然后再配置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);
    }
}
     
     
    
   
  
  
          
<!-- Spring MVC 配置 -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <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>
            <!-- 配置 objectMapper 為我們自定義擴展后的 CustomMapper -->
            <property name="objectMapper">
                <bean class="com.king.framework.jackson.CustomMapper">
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
     
     
    
   
  
  
          

關於屬性 SerializationFeature.FAIL_ON_EMPTY_BEANS 的更多內容不在本文探討范圍內,詳見 Jackson 文檔及源碼。

原文地址:https://blog.csdn.net/kinginblue/article/details/51236938


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM