First, we created a MAVEN project just like before, but since spring Restful Web service is based on Spring 4.x, I've just upgraded spring to 4.0.8, Below I post the main dependencies of my pom file:
<properties><spring.version>4.0.8.RELEASE</spring.version></properties>< Dependencies><dependency><groupid>junit</groupid><artifactid>junit</artifactid ><version>3.8.1</version><scope>test</scope></dependency><dependency> <groupid>org.slf4j</groupid><artifactid>slf4j-log4j12</artifactid><version>1.7.2 </version></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-context</artifactid><version>${spring.version}</version></dependency ><dependency><groupid>org.springframework</groupid><artifactid>spring-webmvc</ Artifactid><version>${spring.version}</version></dependency><dependency><groupid >org.springframework</groupid><artifactid>spring-orm</artifactid><version>${ spring.version}</version></dEpendency><dependency><groupid>org.springframework</groupid><artifactid> Spring-context-support</artifactid><version>${spring.version}</version></dependency> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId> <version>1.8.4</version></dependency><!-- web Project compile-time, deploy to Tomcat without the need for -->< dependency><groupid>javax.servlet</groupid><artifactid>servlet-api</artifactid>< version>2.5</version><scope>provided</scope></dependency><dependency>< groupid>javax.servlet.jsp</groupid><artifactid>jsp-api</artifactid><version>2.2< /version><scope>provided</scope></dependency><!-- web Project compile-time required, deployment to Tomcat is not required --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId> Jackson-databind</artifactid><version>2.3.4</version></dependency><dependency><groupid>org.codehaus.jackson< /groupid><artifactid>jackson-mapper-asl</artifactid><version>1.8.5</version></ Dependency><dependency><groupid>com.fasterxml.jackson.core</groupid><artifactid> Jackson-core</artifactid><version>2.4.3</version></dependency></dependencies> <repositories><repository><id>spring-releases</id><url>https://repo.spring.io/ Libs-release</url></repository></repositories>
There are spring,springmvc,jackson and compile-time Servlet-api and Jsp-api dependencies, there is no other redundant dependencies. Because spring is native support for Jackson, I don't have a choice to use my usual fastjson, or Gson. Of course, if you are using Fastjson, or Gson, in your own project, there is no relationship, replacing the use of the words is also very convenient and simple, here does not do a detailed introduction.
When you add a dependency, you configure the Web file. Creating a RESTful Web service is no different from a normal SPRINGMVC project, so the Web configuration is just as simple as creating a common SPRINGMVC project, simply configuring the servlet and servlet-mapping. Then you specify the SPRINGMVC configuration file and add it to Init-param or Context-param.
<context:component-scan base-package= "Com.yyd.spring.aop.rest.controller"/><beanclass= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/><beanclass=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "><property name=" Messageconverters "><list><bean class=" Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter "/></list></property ></bean>
Here we see that the annotations on the controller are changed to @restcontroller by the @controller we use frequently. @RestControllr is a new annotation in spring 4.x, @RestController, indicating that each method of the class returns an object instead of a view. It is actually a shorthand method for mixed use of @controller and @responsebody. @Responsebody is the function of putting the returned object into the body of the response message ("We all know, wordy.) ")。
You can then start the Tomcat or jetty server and enter it in the browser:
Http://localhost:8080/greeting
You can then see the results returned by the browser,
{"id": 1, "Content": "Hello, world!"}
At this point, a simple spring 4.x-based RESTful Web service framework has been completed, thank you, and hope to help you.
Spring 4.x implements RESTful Web service