Maven project practice based on restful style (converged spring)

Source: Internet
Author: User
Tags assert

We often encounter the problem of finding Java packages during the development of old-fashioned projects, and sometimes the day is gone. MAVEN is the gospel of our development engineers, which can automatically download and add to our projects according to our configuration and publish the corresponding Java packages at the same time. This greatly improves our work efficiency and has the time to learn cutting-edge technology.

What is maven?

MAVEN is dedicated to the configuration management of the project; a project created with Maven must include a pom.xml file to set dependencies, basic configuration of the project (grouid,artifactid,version, etc.), compile the project with plug-ins, environment configuration, Release Management, library configuration, and more. This allows us to focus more on the development of a specific business environment rather than worrying about the configuration of the project. We can quickly build a development framework to manage project configurations through MAVEN.

What is restful?

A software architecture style, a design style rather than a standard, provides only a set of design principles and constraints. It is mainly used for client and server interaction classes of software. The design based on this style of software can be more concise, more layered, more easy to implement caching mechanisms. In fact, to put it bluntly is to let us develop the code less, through very little code to achieve complex functions.

In the MAVEN project, we need to have a pom.xml file; For example, let's start with the project background: This project is primarily a restful-style interface project for use by other module invocations. Development idea: First configure a Pom.xml file and then create the Web. xml file in the Webapp/web-info directory to interact with the data by simulating get, form submission, and post using the spring framework. You can delete the configuration according to your actual requirements.

Pom.xml files are as follows

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>com.seven.spring</groupid><artifactid> Springtest</artifactid><version>0.0.1-snapshot</version><packaging>war</packaging ><!--Define some of the variables that are used in the configuration, Aspect version upgrade--><properties><project.build.sourceencoding>utf-8</ project.build.sourceencoding><contextpath>easydoctor</contextpath><java.version>1.8</ Java.version><json-path-assert.version>2.0.0</json-path-assert.version>We know that spring's Web project requires the creation of a Web. XML document user in the Webapp/web-info directory (class filter, transaction, dispatchseverlet configuration, etc.)

The Web. XML document follows

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "> <!--<listener> &LT;LISTENER-CLASS&GT;COM.S Even. precontextloaderlistener</listener-class> </listener> <context-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath*:meta-inf/spring/root/*.xml</ param-value> </context-param>--> <servlet> <servlet-name>rest</servlet-name> <ser Vlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> < Param-name>contextconfiglocation</param-name> <param-value>classpath*:rest-context.xml</ Param-value> </init-param> &LT;LOAD-ON-STARTUP&GT;1&LT;/LOAD-ON-STARTUP&GT </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/</ url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</ Welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</ Welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</ Welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--< Filter> <display-name>SignerFilter</display-name> <filter-name>signerfilter</filter-name > <filter-class>com.seven.SignerFilter</filter-class> </filter> <filter-mapping> <fi Lter-name>signerfilter</filter-name> <url-pattern>/apiController</url-pattern> <   Dispatcher>request</dispatcher> </filter-mapping> <filter> <display-name>ApiConvertFilter</display-name> <filter-name>apiconvertfilter</filter-name > <filter-class>com.seven.api</filter-class> </filter> <filter-mapping> <filter-name >ApiConvertFilter</filter-name> <url-pattern>/apiController</url-pattern> </ Filter-mapping>--> </web-app>

In the config file for Web. XML,Classpath*:rest-context.xmlthe function is to load this configuration run, often configure some beans in this configuration, used to automatically create the corresponding class object when the container is loaded (generally created objects are singleton mode);

The Rest-context.xml file is as follows:

<?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" xmln S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/        Beans Http://www.springframework.org/schema/beans/spring-beans-4.1.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-4.1.xsd "> <!--annotation tag Controller under scan package Service Restcontroller---<context:component-scan base-package= "com.seven.spring"/> <!--define a Bean object --<bean id= "userdetails" class= "Com.seven.spring.UserDetails"/> <!--auto-Configuration Items--<mvc:annotat Ion-driven/> <context:annotAtion-config/> <!--content Organizer-<bean id= "Contentmanager" class= "org.springframework . Web.accept.ContentNegotiationManagerFactoryBean "> <property name=" favorpathextension "value=" true " /> <property name= "Ignoreacceptheader" value= "true"/> <property name= "Defau Ltcontenttype "value=" text/html "/> <property name=" Usejaf "value=" false "/> &lt ;p roperty name= "MediaTypes" > <map> <entry key= "json" value= "APPL Ication/json "/> <entry key=" html "value=" text/html "/> <ent RY key= "xml" value= "Application/xml"/> </map> </property> & Lt;/bean> <!--JSP View parser configuration Main set modelandview prefix, suffix--<bean id= "jspviewresolver" class= "org . springframework.web.serVlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf/jsp/"/> <p   Roperty name= "suffix" value= ". jsp"/> </bean> </beans>

After the basic configuration is written code, I mainly do some simple interface test; the relevant code is as follows

Package Com.seven.spring;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.http.httpstatus;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.requestparam;import Org.springframework.web.bind.annotation.responsestatus;import     Org.springframework.web.bind.annotation.RestController;     @RestController public class Springrestcontrollerdemo {@Autowired userdetails userdetails; /* @RequestMapping (value= "/springcontent", method=requestmethod.get,produces={"Application/xml", "application/ JSON "}) @RequestMapping (value="/springcontent.json ", method=requestmethod.get) */@RequestMapping (v           Alue= "/springcontent", Method=requestmethod.post) @ResponseStatus (httpstatus.ok) public userdetails GetUser () { Userdetails userdetails =New Userdetails ();           Userdetails.setusername ("seven");           Userdetails.setemailid ("[email protected]");       return userdetails; } @RequestMapping (value = "Springcontentadd", method = requestmethod.post) public userdetails AddUser (@Reques        TBody userdetails userdetails) {System.out.println ("userdetail=" +userdetails);    return userdetails; } @RequestMapping (value = "Springcontentpar", method = requestmethod.post) public String Getrquerypar (@RequestPa        Ram ("name") String userdetails) {System.out.println ("userdetail=" +userdetails);    return userdetails; } @RequestMapping (value= "/springcontent.htm", Method=requestmethod.get) @ResponseStatus (Httpstatus.ok) PU       Blic String getuserhtml () {//test HTML view return "Example";   }   }

Everyone in their own native creation try.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Maven project practice based on restful style (converged spring)

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.