5分钟构建spring web mvc REST风格HelloWorld

本文介绍如何使用Spring MVC快速构建REST风格的应用程序。通过Maven创建项目并添加必要的依赖,如Spring WebMVC、Jackson等,并利用Jetty插件进行本地测试。文中还提供了实体类、控制器及配置的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

当然写本文的目的不是为了速度,只是表明现在构建一个Spring web mvc Rest风格的HelloWorld应用会很简单。不过如果看过Spring Boot这个项目,可能只需要最多3分钟就能构建一个简单的Rest风格应用。回头研究下,然后分享下。

 

我的构建环境

JDK 7

Maven 3

Servlet3容器

 

创建项目

首先使用Maven创建一个普通Maven应用即可,不必是web的。

 

添加依赖

Java代码   收藏代码
  1. <!-- servlet 3 -->  
  2. <dependency>  
  3.     <groupId>javax.servlet</groupId>  
  4.     <artifactId>javax.servlet-api</artifactId>  
  5.     <version>3.0.1</version>  
  6. </dependency>  
  7.   
  8. <!--spring context -->  
  9. <dependency>  
  10.     <groupId>org.springframework</groupId>  
  11.     <artifactId>spring-context-support</artifactId>  
  12.     <version>4.0.0.RELEASE</version>  
  13. </dependency>  
  14.   
  15. <!--spring webmvc -->  
  16. <dependency>  
  17.     <groupId>org.springframework</groupId>  
  18.     <artifactId>spring-webmvc</artifactId>  
  19.     <version>4.0.0.RELEASE</version>  
  20. </dependency>  
  21.   
  22. <!--jackson -->  
  23. <dependency>  
  24.     <groupId>com.fasterxml.jackson.core</groupId>  
  25.     <artifactId>jackson-databind</artifactId>  
  26.     <version>2.2.3</version>  
  27. </dependency>  

 servlet3依赖scope是provided表示环境提供,然后添加spring-context-support和spring-webmvc依赖,最后用于json的jackson依赖。非常简单明了。

 

添加maven插件

为了方便测试,添加jetty的maven插件,这样直接使用mvn jetty:run即可运行。

Java代码   收藏代码
  1. <build>  
  2.     <finalName>springmvc</finalName>  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.mortbay.jetty</groupId>  
  6.             <artifactId>jetty-maven-plugin</artifactId>  
  7.             <version>8.1.8.v20121106</version>  
  8.             <configuration>  
  9.                 <reload>manual</reload>  
  10.                 <webAppConfig>  
  11.                     <contextPath>/${project.build.finalName}</contextPath>  
  12.                 </webAppConfig>  
  13.                 <connectors>  
  14.                     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  15.                         <port>9080</port>  
  16.                         <!--<maxIdleTime>60000</maxIdleTime>-->  
  17.                     </connector>  
  18.                 </connectors>  
  19.             </configuration>  
  20.         </plugin>  
  21.     </plugins>  
  22. </build>  

  

实体 

Java代码   收藏代码
  1. package com.sishuok.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * <p>User: Zhang Kaitao 
  7.  * <p>Date: 13-12-19 
  8.  * <p>Version: 1.0 
  9.  */  
  10. public class User implements Serializable {  
  11.     private Long id;  
  12.     private String name;  
  13.   
  14.     public Long getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(Long id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29.   
  30.     @Override  
  31.     public boolean equals(Object o) {  
  32.         if (this == o) return true;  
  33.         if (o == null || getClass() != o.getClass()) return false;  
  34.   
  35.         User user = (User) o;  
  36.   
  37.         if (id != null ? !id.equals(user.id) : user.id != nullreturn false;  
  38.   
  39.         return true;  
  40.     }  
  41.   
  42.     @Override  
  43.     public int hashCode() {  
  44.         return id != null ? id.hashCode() : 0;  
  45.     }  
  46. }  

 

控制器

Java代码   收藏代码
  1. package com.sishuok.controller;  
  2.   
  3. import com.sishuok.entity.User;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. /** 
  10.  * <p>User: Zhang Kaitao 
  11.  * <p>Date: 13-12-19 
  12.  * <p>Version: 1.0 
  13.  */  
  14. @RestController  
  15. @RequestMapping("/user")  
  16. public class UserController {  
  17.   
  18.     @RequestMapping(value = "/{id}", method = RequestMethod.GET)  
  19.     public User view(@PathVariable("id") Long id) {  
  20.         User user = new User();  
  21.         user.setId(id);  
  22.         user.setName("zhang");  
  23.         return user;  
  24.     }  
  25. }  

 

pringMVC注解风格配置

Java代码   收藏代码
  1. @Configuration  
  2. @EnableWebMvc  
  3. @ComponentScan  
  4. public class AppConfig {  
  5. }  

具体含义请参考《Spring4新特性——Groovy Bean定义DSL》部分。

 

Servlet3容器启动初始化器

在Servlet容器启动时编程式注册Servlet

Java代码   收藏代码
  1. package com.sishuok;  
  2.   
  3. import org.springframework.web.WebApplicationInitializer;  
  4. import org.springframework.web.context.WebApplicationContext;  
  5. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;  
  6. import org.springframework.web.servlet.DispatcherServlet;  
  7.   
  8. import javax.servlet.ServletContext;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.ServletRegistration;  
  11.   
  12. /** 
  13.  * <p>User: Zhang Kaitao 
  14.  * <p>Date: 13-12-19 
  15.  * <p>Version: 1.0 
  16.  */  
  17. public class AppInitializer implements WebApplicationInitializer {  
  18.   
  19.     @Override  
  20.     public void onStartup(ServletContext servletContext) throws ServletException {  
  21.         AnnotationConfigWebApplicationContext webApplicationContext =  
  22.                 new AnnotationConfigWebApplicationContext();  
  23.         webApplicationContext.register(AppConfig.class);  
  24.   
  25.   
  26.         DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);  
  27.         ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet);  
  28.         dynamic.addMapping("/");  
  29.   
  30.     }  
  31. }   

然后运行 mvn jetty:run运行即可,浏览器输入如下地址即可得到我们的json数据。

http://localhost:9080/springmvc/user/1

 参考示例的github地址:springmvc-rest-helloworld

 

非常简单的一个Rest风格的web应用就搭建完了,接下来再完善即可。

 

下一篇将介绍使用Spring Boot 2分钟构建spring mvc REST风格HelloWorld。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值