5分钟构建spring web mvc REST风格HelloWorld
<!-- 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依赖scope是provided表示环境提供,然后添加spring-context-support和spring-webmvc依赖,最后用于json的jackson依赖。非常简单明了。
?
添加maven插件为了方便测试,添加jetty的maven插件,这样直接使用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>
??
实体?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; }}
?
控制器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; }}
具体就不多介绍了,请参考:《Spring4新特性——Web开发的增强》和《跟我学SpringMVC?》。
?
?
SpringMVC注解风格配置@Configuration@EnableWebMvc@ComponentScanpublic class AppConfig {}
具体含义请参考《Spring4新特性——Groovy Bean定义DSL》部分。
?
Servlet3容器启动初始化器在Servlet容器启动时编程式注册Servlet
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("/"); }}?
具体含义请参考《跟我学Spring3》的第12章?和《Spring4新特性——Groovy Bean定义DSL》和我的github中的《servlet3-showcase》部分。?
?
然后运行 mvn jetty:run运行即可,浏览器输入如下地址即可得到我们的json数据。
http://localhost:9080/springmvc/user/1
?
参考示例的github地址:springmvc-rest-helloworld
?
非常简单的一个Rest风格的web应用就搭建完了,接下来再完善即可。
?
下一篇将介绍使用Spring Boot 2分钟构建spring mvc REST风格HelloWorld。
?
?
还在吧?这里有个springmvc rest的问题,http://www.oschina.net/question/99204_139192,请帮忙解答一下。 还在吧?这里有个springmvc rest的问题,http://www.oschina.net/question/99204_139192,请帮忙解答一下。