Spring MVC supports restful style attempts

Source: Internet
Author: User
Tags bind config json xmlns java web
Spring MVC Core Process
Today's Java Web development is largely different from the one we learned at school, and now the most mature companies use spring MVC to quickly develop Web applications. Of course, I am also **spring mvc** long-term practitioners.
first of all, I would like to popularize the spring MCV in my eyes the entire process process

the restful style of Spring MVC

To tell you the truth, I used this long spring MVC only to know that the link format he requested was consistent with the restful style, but did not think about it at the same time as Resteasy, which supported the standard Jax, and then took everyone to understand, and pointed out that there might be a pit ha
Build SPRINGMVC Frame
This system uses the development tool is the idea simultaneously uses the SPRINGMVC +maven the way realizes the simple demo
Don't say much nonsense, first show you the project structure

Projects and mostly projects, Java decentralized classes, resources put some configuration files and resource, WebApp storage static resources
Of course, here we need to emphasize the configuration of Web. xml
<web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_ 2_4.xsd "> <display-name>spring MVC application</display-name> <servlet> <servlet -name>springservlet</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-n Ame>contextconfiglocation</param-name> <param-value>classpath*:mvc-dispatcher-servlet.xml</p aram-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> & lt;! --the difference between config///* is whether static resources are blocked such as .jsp--> <servlet-mapping> <servlet-name>springservlet</servlet-name > <url-pattern>/</url-pattern> </servlet-mapping> </Web-app> 

Pom files also need to be noted, if you want to support the JSON format of restful, you need to import Jackson's related packages, and later on the implementation of the principle will be further explained

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.springapp</groupId> <artifactid>springwebflow</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>
        Springwebflow</name> <properties> <spring.version>4.1.1.RELEASE</spring.version> <jackson.version>2.1.4</jackson.version> </properties> <dependencies> &LT;DEP Endency> <groupId>org.springframework</groupId> <artifactid>spring-core</a rtifactid> <version>${spring.version}</version> </dependency> <depend Ency> <groupid&Gt;org.springframework</groupid> <artifactId>spring-web</artifactId> <version >${spring.version}</version> </dependency> <dependency> <groupid>j Avax.servlet</groupid> <artifactId>servlet-api</artifactId> &LT;VERSION&GT;2.5&L t;/version> </dependency> <dependency> <groupid>javax.servlet.jsp</gr
            Oupid> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid&gt ;org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <versio n>${spring.version}</version> </dependency> <dependency> <groupId> org.springframework</Groupid> <artifactId>spring-test</artifactId> <version>${spring.version}</
            version> <scope>test</scope> </dependency> <dependency>
            <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> &LT;ARTIFACTID&GT;JACKSON-DATABIND&LT;/ARTIFAC tid> <version>${jackson.version}</version> </dependency> <dependency&
            Gt <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-annotations</artifactid
        > <version>${jackson.version}</version> </dependency> <dependency>    <groupId>com.fasterxml.jackson.module</groupId> <artifactid>jackson-module-jaxb-annotati Ons</artifactid> <version>${jackson.version}</version> </dependency> & Lt;dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactid>jac Kson-jaxrs-json-provider</artifactid> <version>${jackson.version}</version> < !--Jackson2.2.0 version is not supported for CXF2.7.3-<!--http://www.marshut.com/krrqx/fasterxml-jackson-2-2-provider- No-longer-works-with-cxf-jax-rs.pdf-<!--Http://cxf.547215.n5.nabble.com/Creating-input-values-on-WA Dl-td5728910.html-</dependency> <!--Spring Webmvc 3.1.2 to <dependency > <groupId>org.codehaus.jackson</groupId> <artifactid>jackson-mapper-asl</
         Artifactid>   <version>1.9.0</version> </dependency> <dependency> <groupId> Org.codehaus.jackson</groupid> <artifactId>jackson-core-asl</artifactId> <ver sion>1.9.0</version> </dependency> <dependency> <groupid>junit&lt
            ;/groupid> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> &L t;finalname>springwebflow</finalname> <plugins> <plugin> <arti Factid>maven-compiler-plugin</artifactid> <configuration> <source&gt
            ;1.6</source> <target>1.6</target> </configuration>
      </plugin>      <plugin> <artifactId>maven-surefire-plugin</artifactId> <config
                    Uration> <includes> <include>**/*Tests.java</include> </includes> </configuration> </plugin> </plugins > </build> </project>

The last one to be aware of is the spring configuration file, which needs to add the MVC automatic driver annotations, otherwise some annotations will not be used

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" XMLNS:MV
       C= "Http://www.springframework.org/schema/mvc" xmlns:context= "Http://www.springframework.org/schema/context"
       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util.xsd "> <mvc:annotation-driven/> <context:component-scan base-package=" COM.SPRINGAPP.MVC "/ > <bean class= "Org.springframework.web.servlet.view.InternalResourceVieWresolver "> <property name=" prefix "value="/web-inf/pages/"/> <property name=" suffix "value=". JSP "/> </bean> </beans>

And finally the controller code.

Package COM.SPRINGAPP.MVC;

Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import Org.springframework.web.bind.annotation.RequestBody;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping ("/test") public
class Hellocontroller {

    @RequestMapping (value = "/hello", METHOD = requestmethod.get) public
    String Printwelcome (Modelmap model) {
        Model.addattribute ("message", " Hello world! ");
        return "Hello";
    }

    @RequestMapping (value = "UserInfo", method = Requestmethod.post)
    @ResponseBody public person  UserInfo (@RequestBody person person) {

        System.out.println ("Person's name:" +person.getname () + ", Age of Man" +person.getage ());

        return person;
    }
}

Here you can see that using @requestbody indicates that the accepted request is advanced over the message Converter (HTTP conversion), and the message converter parses the request. Of course, you can also parse which format Contest-type format by consumes the specified value
This is also why you need to import the Jackson package mentioned above, the internal message converter uses the Jackson way to convert

Finally, of course, the call was made with postman haha

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.