springmvc配置jackson時遇到的一些問題


在沒接觸springmvc之前我們在servlet中想返回前台json數據時,都是自定義一個JSONObject和JSONArray,然后調用response.getWriter()對象的方法返回json數據,類似:

1 jsonObject.element("photoAudit", audit);
2 jsonObject.element("photoAudit2", audit2);
3 jsonObject.element("photoAudit3", audit3);
4 jsonArrayR.add(jsonObject);
5 
6 
7 PrintWriter out = response.getWriter();
8 out.print(jsonR.toString());
9 out.close();

在接觸springmvc項目后,知道可以springmvc可以通過配置讓對象、list集合或者map可以直接返回json格式的數據,非常方便。如果自己配置的話,還是需要自己多了解一些,不要盲目的在網上直接copy代碼就結束了。我們在配置springmvc配置文件的時候,一定會把controller的包加入的,就是我們的控制器,如下:

<context:component-scan base-package="com.example.controller"/>

但是我們往往不會注意一句很簡單的代碼:

1 <mvc:annotation-driven/>

這句代碼一般情況是直接跟在上面那句掃描controller包后面的。從字面理解的意思就是mvc的注解驅動。<mvc:annotation-driven/>會自動注冊兩個bean,分別為DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter。是springmvc為@controller分發請求所必須的。除了注冊了這兩個bean,還提供了很多支持,其中一項就提到了讀寫JSON的支持(Jackson)。

關於配置springmvc的jackson依賴,有主要兩個步驟(除了jackson還可以配置阿里的fastjson),第一步就是在添加jar包依賴,我配置的是jackson2的版本,如下:

 1 <!--jackson-->
 2     <dependency>
 3       <groupId>com.fasterxml.jackson.core</groupId>
 4       <artifactId>jackson-core</artifactId>
 5       <version>2.7.0</version>
 6     </dependency>
 7     <dependency>
 8       <groupId>com.fasterxml.jackson.core</groupId>
 9       <artifactId>jackson-databind</artifactId>
10       <version>2.7.0</version>
11     </dependency>
12     <dependency>
13       <groupId>com.fasterxml.jackson.core</groupId>
14       <artifactId>jackson-annotations</artifactId>
15       <version>2.7.0</version>
16     </dependency>

第二步就是在springmvc的配置文件中加入轉換器的配置,這里其實不加配置只有<mvc:annotation-driven/>的話也可以實現json自動轉化,但是會面臨一個問題,就是在使用IE瀏覽器時,返回的數據格式如果是json的話,會出現提示下載的頁面。我們要解決這個問題。網上給的幾種方案。

方案一:

在springmvc的配置文件中添加如下代碼

<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>text/html;charset=UTF-8</value>
   </list>
  </property>
</bean>

這種一看就覺得不對,定義了一個轉化器,但是只是定義了一個轉化器而已,什么關聯都沒有。首先我們要了解問題出現的原因,才能夠解決問題。springmvc返回json數據在IE瀏覽器中訪問,會出現下載現象是因為IE10以下不支持application/json格式的Response響應,也就是說低於IE10版本的IE瀏覽器都需要使用text/html的Response響應。所以我們知道了轉換器肯定要綁定response才能夠實現。

方案二:

在springmvc的配置文件中添加如下代碼

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  <property name="messageConverters">
   <list >
    <ref bean="mappingJacksonHttpMessageConverter" />
   </list>
  </property>
 </bean>
 <bean id="mappingJacksonHttpMessageConverter"
  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>text/html;charset=UTF-8</value>
   </list>
  </property>
 </bean> 

這種感覺已經很對了,而且查了一下大多數網上給的都是這種。我在添加了如上代碼后測試還是會提示下載,然后就在想會不會是RequestMappingHandlerAdapter的問題,上網查了一下RequestMappingHandlerAdapter的作用。RequestMappingHandlerAdapter的作用大概是簡單來說就是采用反射機制調用url請求對應的Controller中的方法(這其中還包括參數處理等等操作沒有介紹),返回執行結果值,這樣就完成了HandlerAdapter的使命。看來RequestMappingHandlerAdapter的作用就是負責分發到controller並返回的過程,這么看的話添加上面的代碼應該可以才對,不明白為什么不行。最后把代碼改為:

<mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="stringHttpMessageConverter"/>
            <ref bean="mappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean id="stringHttpMessageConverter"
          class="org.springframework.http.converter.StringHttpMessageConverter"/>

    <!--解決IE瀏覽器json文件下載和json數據中午亂碼的問題-->
    <bean id="mappingJackson2HttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

其實就是相當於把jackson的轉化器從原來添加到RequestMappingHandlerAdpter改為直接添加到注解驅動<mvc:annotation-driven>里,在<mvc:annotation-driven></mvc:annotation-driven>添加<mvc:message-converters>消息轉化器,將自定義的轉換器加入其中就OK了。注:這里定義的轉化器bean的id都是自定義的,你願意起什么名字就起什么名字。但是為什么要這樣才行我還不清楚,先記錄一下,然后再查資料弄懂這個問題。


免責聲明!

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



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