标签:
第一步:创建一个Maven工程。加入Jersey、Spring、Jersey+Spring的依赖包,以及内嵌的Tomcat7插件;
pom.xml文件如图所示:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zte.ems</groupId> <artifactId>jersey-spring</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>jersey-spring Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- Jersey --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.5.RELEASE</version> </dependency> <!-- Jersey + Spring --> <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-spring</artifactId> <version>1.8</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加jsp、servlet依赖 --> <!-- servlet api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <build> <finalName>jersey-spring</finalName> <pluginManagement> <plugins> <plugin> <!-- 配置tomcat7插件 --> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/jersey-spring</path> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
第二步:Spring配置文件的创建:很简单,就是添加需要扫描的基本包名
spring-context.xml文件如下所示:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.zte.ems.*" /> <context:annotation-config/> </beans>
第三步:配置web.xml文件:主要是配置spring和spring+jersey的相关信息;
web.xml文件如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置Spring --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <!-- 配置Jersey+Spring --> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.zte.ems.resource</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
其中 <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>这一句常常出错,需要多注意。
第四步:写rest资源类
在com.zte.ems.resource包下添加一个StudentResource.java类,代码如下:
package com.zte.ems.resource; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.zte.ems.pojo.Student; import com.zte.ems.service.StudentService; @Component @Path("/index") public class StudentResource { @Autowired StudentService studentService; @GET @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_ATOM_XML}) @Path("/student") public String getStudent(){ return "hello Student!"; } }
@Component:表示这个类由Spring的IoC容器来管理,SpringMVC中使用的@Controller;
@AutoWired:表示Spring的自动装配,将需要依赖的类直接装配进来,当然本例子没有使用到,具体项目中一定会需要;
@Path("/index"):该注解标记在类名之上,表示这是一个根资源;类似于SpringMVC中的@RequestMapping("/index");
@Path("/student"):该注解标记在方法名之上,表示这是一个子资源,通过/student来寻找具体方法调用;
@GET:该注解表示被标记的方法提供的是一个GET请求,同样还有@POST、@PUT、@DELET、@PUTCH等注解;
@Produces({MediaType.xxx}):该注解表示以json或者xml文件的形式进行输出,类似于SpringMVC中的@ResponseBody注解;
当然还有@QueryParam,@Context等等注解,后面会专门写一篇关于Jersey和SpringMVC之间区别和联系的文章,敬请期待。
最后:
鼠标放在项目名上右击,Run As ----> Maven build ---->在Goals中输入clean tomcat7:run启动Tomcat服务器,然后在浏览器中输入http://localhost:8080/jersey-spring/index/student 弹出下图所示的成功界面:
到此为止,基于Jersey+Spring+Maven集成的简单框架搭建起来了。
标签:
原文地址:http://www.cnblogs.com/wrong5566/p/5982992.html