如何讓Spring MVC接收的參數可以轉換為java對象

來源:互聯網
上載者:User

標籤:request   預設   dha   問題   data   work   sage   java類   load   

情境:

  web.xml中增加了一個DispatcherServlet配置,並在同級目錄下添加了**-servlert.xml檔案,搭建起了一個spring mvc的restful提供者。

 

問題描述:

Controller的@RequestBody,

  如果參數定義類型為String,則可以擷取到資料;

  如果參數定義類型為其他java對象,就接收不到。

 

下面記錄完整的解決方案:

1. web.xml

  <!-- spring mvc依賴的大環境,此參數會被ContextLoaderListener使用 -->
  <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicatonContext.xml</param-value> </context-param>
    
  <!-- 模組化介面的配置 -->
  <servlet> <servlet-name>rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/rest-servlet.xml</param-value> <!-- 模組化介面的設定檔位置,此處填寫的是預設地址,也可指向其他位置 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>

 

2. rest-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation=" http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">    <mvc:annotation-driven /> <!-- 開啟註解掃描 -->    <context:component-scan base-package="com.springmvc.rest" />  <!-- 指定需要掃描的package --></beans>

 

 3. applicatonContext.xml

添加如下的一段配置:

作用:指定json型別參數的轉換器,可將接收到的json類型的參數,封裝為java對象。(去掉這個配置就會報錯)

    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">      <property name="transactionManager" ref="transactionManager"/>    </bean>    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">          <property name="messageConverters">              <list>                  <ref bean="jsonHttpMessageConverter" />            </list>        </property>      </bean>    <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">          <property name="supportedMediaTypes">              <list>                  <value>application/json;charset=UTF-8</value>              </list>          </property>    </bean>

 

4. pom.xml

添加三個jackson依賴jar包。(spring MVC的jar,也需添加依賴,此處不描述)

作用:spring mvc底層解析json成java類,用的就是jsckson的jar包,不依賴它肯定也是無法成功解析的啦。

   <dependency>        <groupId>com.fasterxml.jackson.core</groupId>        <artifactId>jackson-databind</artifactId>        <version>2.4.1</version>    </dependency>    <dependency>           <groupId>org.codehaus.jackson</groupId>           <artifactId>jackson-core-lgpl</artifactId>           <version>1.8.1</version>        </dependency>       <dependency>           <groupId>org.codehaus.jackson</groupId>           <artifactId>jackson-mapper-lgpl</artifactId>           <version>1.8.1</version>     </dependency>

 

5. Controller的方法

  指定接收參數類型為application/json。(我把這個指定去掉也正確接收了,還是加上吧)

    @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/json")    public @ResponseBody RequestResult test(@RequestBody User user) {        // ....        }

 

至此,@RequestBody就可以正確的接收到參數了。

 

如何讓Spring MVC接收的參數可以轉換為java對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.