SpringMVC实现Restful风格的WebService
1.环境
- JDK7
- MyEclipse2014
- tomcat8
- maven 3.3.3
- spring4.1.4
2.创建maven工程
使用MyEclipse创建maven工程的方式可以参考这篇博文(链接1), 该博文中的第四小结是关于如何创建SpringMVC+maven教程。下面只给出创建好的目录结构,其中的部分文件如java程序是后面要完成的。
3.指定依赖文件
maven具有特定的文件结构,并通过pom.xml来管理工程。下面是在实现Restful时需要的依赖。
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.dy.xidian</groupId> 4 <artifactId>transport</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <name>transport Maven Webapp</name> 8 <url>http://maven.apache.org</url> 9 10 <properties> 11 <spring.version>4.1.4.RELEASE</spring.version> 12 </properties> 13 14 <dependencies> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>3.8.1</version> 19 <scope>test</scope> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-core</artifactId> 24 <version>${spring.version}</version> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-context</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-webmvc</artifactId> 34 <version>${spring.version}</version> 35 </dependency> 36 <!-- web项目编译时需要,部署到tomcat中就不需要了 --> 37 <dependency> 38 <groupId>javax.servlet</groupId> 39 <artifactId>servlet-api</artifactId> 40 <version>2.5</version> 41 <scope>provided</scope> 42 </dependency> 43 <dependency> 44 <groupId>javax.servlet.jsp</groupId> 45 <artifactId>jsp-api</artifactId> 46 <version>2.2</version> 47 <scope>provided</scope> 48 </dependency> 49 50 <dependency> 51 <groupId>com.fasterxml.jackson.core</groupId> 52 <artifactId>jackson-databind</artifactId> 53 <version>2.3.4</version> 54 </dependency> 55 <dependency> 56 <groupId>com.fasterxml.jackson.core</groupId> 57 <artifactId>jackson-annotations</artifactId> 58 <version>2.4.0</version> 59 </dependency> 60 <dependency> 61 <groupId>com.fasterxml.jackson.core</groupId> 62 <artifactId>jackson-core</artifactId> 63 <version>2.4.3</version> 64 </dependency> 65 </dependencies> 66 67 <build> 68 <finalName>transport</finalName> 69 </build> 70 </project>
4.配置web.xml
web.xml的配置过程十分简单,只需指定dispatchServlet即可。
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>/WEB-INF/transport-servlet.xml</param-value> 11 </context-param> 12 <servlet> 13 <servlet-name>transport</servlet-name> 14 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>transport</servlet-name> 19 <url-pattern>/</url-pattern> 20 </servlet-mapping> 21 <listener> 22 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 23 </listener> 24 </web-app>
5.配置spring的配置文件(spring-servlet.xml)
配置该文件时,约束文档要指定成spring4的约束,因为spring4实现了对restful的支持。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 12 13 <mvc:annotation-driven /> 14 <context:component-scan base-package="com.dy.xidian" /> 15 </beans>
6.controller的实现
前面是已经把准备工作做完了,下面进入编程阶段。关于Restful风格的webService相信大家已经做过功课了,这里不在累述。因为应答是json格式,所以我们需要进行下面的工作。
6.1建立bean类
在开始的图片中,我们可以看到有个Greeting.java类,它其实就是一个bean类,具体代码如下。
1 package com.dy.xidian; 2 3 public class Greeting { 4 private final long id; 5 private final String content; 6 7 public Greeting(long id, String content) { 8 this.id = id; 9 this.content = content; 10 } 11 12 public long getId() { 13 return id; 14 } 15 16 public String getContent() { 17 return content; 18 } 19 }
6.2 在控制器中处理请求
SpringMVC会将请求交给一个控制器来处理,所以我们必须实现这样一个控制器,不过这个控制器在编写时要遵循restful风格。
GreetingController.java
1 package com.dy.xidian; 2 3 import java.util.concurrent.atomic.AtomicLong; 4 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestParam; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 public class GreetingController { 11 private static final String template = "Hello, %s!"; 12 private final AtomicLong counter = new AtomicLong(); 13 14 @RequestMapping("/getuser") 15 public Greeting greeting( 16 @RequestParam(value = "name", defaultValue = "World") String name) { 17 return new Greeting(counter.incrementAndGet(), String.format(template, 18 name)); 19 } 20 }
虽然控制器返回的是一个Greeting对象,但是Spring会通过Jackson工具将其转换为一个json串。
7.访问
注意访问时输入的URL
8.参考文章
博文1:http://bbs.51cto.com/thread-1131636-1.html 博文2:http://www.blogjava.net/soken/articles/371548.html 博文3:http://wiselyman.iteye.com/blog/2002446