How to let spring MVC receive parameters that can be converted to Java objects

Source: Internet
Author: User

Scene:

A dispatcherservlet configuration was added to Web. XML and a **-servlert.xml file was added to the sibling directory, and a spring MVC RESTful access interface was built.

Problem Description:

Controller's @requestbody,

If the parameter definition type is string, the data can be obtained;

If the parameter definition type is another Java object, it is not received.

The complete workaround is documented below:

1. Web. xml

<!--the big environment that spring MVC relies on, this parameter is Contextloaderlistener used
<
Context-param> <param-name> Contextconfiglocation</param-name> <Param-value >classpath:applicatonContext.xml</param-value> </context-param>
    
   <!--Modular Interface configuration--
<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> <!--the configuration file location of the modular interface, which is filled with the default address, or to another location -- </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

<Beansxmlns= "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/> <!--open annotation scan --    <Context:component-scanBase-package= "Com.springmvc.rest" /> <!--Specify the package that needs to be scanned--</Beans>

3. Applicatoncontext.xml

Add the following configuration:

Function: A converter that specifies the JSON type parameter, which encapsulates the parameters of the JSON type that is received as a Java object. (remove this configuration will be error)

    <BeanID= "Transactiontemplate"class= "Org.springframework.transaction.support.TransactionTemplate">      < Propertyname= "TransactionManager"ref= "TransactionManager"/>    </Bean>    <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">          < Propertyname= "Messageconverters">              <List>                  <refBean= "Jsonhttpmessageconverter" />            </List>        </ Property>      </Bean>    <BeanID= "Jsonhttpmessageconverter"class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">          < Propertyname= "Supportedmediatypes">              <List>                  <value>Application/json;charset=utf-8</value>              </List>          </ Property>    </Bean>

4. Pom.xml

Add three Jackson-dependent jar packages. (Spring MVC jar, also need to add dependencies, not described here)

Role:spring MVC bottom parsing json into Java class, with the Jsckson jar package, do not rely on it must also be unable to parse successfully.

     <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's method

  specifies that the receive parameter type is Application/json. (I removed the designation and received it correctly, or add it)

    consumes = "Application/json" )    public  @ResponseBody requestresult Test (@RequestBody user user) {        //  ....        }

At this point, the @RequestBody can receive the parameters correctly.

How to let spring MVC receive parameters that can be converted to Java objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.