Of course, the goal of this article is not to be speed, but to show that it is now easy to build a Spring web mvc Rest-style HelloWorld application.However, if you've seen the Spring Boot project, it may take up to three minutes to build a simple Rest-style application.Go back and share.
My Building Environment
JDK 7
Maven 3
Servlet3 Container
Create Project
First use Maven to create a normal Maven application, not a web application.
Add Dependency
<!-- servlet 3 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <!--spring context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!--spring webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!--jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.3</version> </dependency>
servlet3 dependency scope is provided to represent environment provisioning, then adds spring-context-support and spring-webmvc dependencies, and finally jackson dependencies for json.It's very simple and clear.
Add a maven plugin
For testing purposes, add a jetty maven plug-in so that it runs directly using mvn jetty:run.
<build> <finalName>springmvc</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.8.v20121106</version> <configuration> <reload>manual</reload> <webAppConfig> <contextPath>/${project.build.finalName}</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>9080</port> <!--<maxIdleTime>60000</maxIdleTime>--> </connector> </connectors> </configuration> </plugin> </plugins> </build>
Entities
package com.sishuok.entity; import java.io.Serializable; /** * <p>User: Zhang Kaitao * <p>Date: 13-12-19 * <p>Version: 1.0 */ public class User implements Serializable { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
Controller
package com.sishuok.controller; import com.sishuok.entity.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * <p>User: Zhang Kaitao * <p>Date: 13-12-19 * <p>Version: 1.0 */ @RestController @RequestMapping("/user") public class UserController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User view(@PathVariable("id") Long id) { User user = new User(); user.setId(id); user.setName("zhang"); return user; } }
Specifically, do not introduce more, please refer to: New Spring4 Feature - Enhancement of Web Development "And" Learn Spring MVC from me>.
SpringMVC Annotation Style Configuration
@Configuration @EnableWebMvc @ComponentScan public class AppConfig { }
Refer to the specific meaning of " New Spring4 Feature - Groovy Bean Definition DSL "Section.
Servlet3 Container Start Initializer
Programmatically register Servlet at Servlet container startup
package com.sishuok; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; /** * <p>User: Zhang Kaitao * <p>Date: 13-12-19 * <p>Version: 1.0 */ public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); webApplicationContext.register(AppConfig.class); DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext); ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet); dynamic.addMapping("/"); } }
Refer to the specific meaning of " Learn Spring3 from me Of Chapter 12 And New Spring4 Feature - Groovy Bean Definition DSL And My github servlet3-showcase "Section.(
Then run mvn jetty:run and your browser will enter the following address to get our json data.
http://localhost:9080/springmvc/user/1
Reference example github address: springmvc-rest-helloworld
A very simple Rest-style web application is set up and ready to go.
The next article introduces building a spring mvc REST-style HelloWorld using Spring Boot in 2 minutes.
Reprinted at: https://my.oschina.net/qjx1208/blog/200929