Spring集成Jersey开发,实现rest约束实例
读这篇文章的前提是我们应当知道Jersey框架是对RESTful约束的一个实现的框架,这里假设大家都明白Jersey框架以及RESTful约束。项目管理使用Maven。当然Spring相关知识是基础,你应该会,不然还搞啥技术?
这篇文章的目的是为了在Jersey使用Spring的注解功能,为了让Jersey能使用Spring管理的实例。
创建项目名是JerseySpring的maven项目
注意“archetype”选择”maven-archetype-quickstart”
项目结构如下:
刚创建的maven是自动设置成打包“jar”的项目,所以需要设置成’war’。
1.项目右键选择:Properties->Project Facets->Convert to facted form…按下图红框标注选择:
点确定,完成javaweb项目转换,如果maven没设置编译水平是高版本的话,默认是1.5的,当然太低了,这里需要设置。
设置“java compiler和java build buildPath”就可以了,然后pom.xml里设置插件编译水平比如:1.7.如下:
<build>
<finalName>JerseyfinalName>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>${java.version}source>
<target>${java.version}target>
configuration>
plugin>
plugins>
build>
如果想统一设置,那在创建项目之前设置maven的默认编译水平。在maven的配置文件conf/setting.xml里添加如下:
<profile>
<id>jdk17id>
<activation>
<activeByDefault>trueactiveByDefault>
<jdk>1.7jdk>
activation>
<properties>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<maven.compiler.compilerVersion>1.7maven.compiler.compilerVersion>
properties>
profile>
spring使用4.2.0.RELEASE,jersey-spring使用1.18版本。
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.jaxgroupId>
<artifactId>JerseySpringartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>JerseySpringname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.7java.version>
<java.encoding>UTF-8java.encoding>
<spring-version>4.2.0.RELEASEspring-version>
<aspectj-version>1.8.6aspectj-version>
<jersey-spring>1.18jersey-spring>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-websocketartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-messagingartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring-version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjrtartifactId>
<version>${aspectj-version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>${aspectj-version}version>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>3.1version>
dependency>
<dependency>
<groupId>com.sun.jersey.contribsgroupId>
<artifactId>jersey-springartifactId>
<version>${jersey-spring}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<finalName>JerseyfinalName>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>${java.version}source>
<target>${java.version}target>
configuration>
plugin>
plugins>
build>
project>
pom.xml文件里引入的spring框架相关jar和jersey-spring.jar。不需要引入jersey-container-servlet-core.jar。
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
"com.jax" />
扫描com.jar包下所以注解,包括spring注解和jersey注解,这里有个坑,如果单单扫描spring的注解,不扫描jersey注解是会报错的,所以base-package尽量是大范围的,把jersey资源文件也扫描进去。
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JerseySpringdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>jersey-serlvetservlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServletservlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packagesparam-name>
<param-value>com.jax.Jerseyparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>jersey-serlvetservlet-name>
<url-pattern>/api/*url-pattern>
servlet-mapping>
web-app>
加载了applicationContext.xml文件,设置编码过滤器为utf-8,
使用“com.sun.jersey.spi.spring.container.servlet.SpringServlet”创建servlet,这里注意
1.
param>
<param-name>com.sun.jersey.config.property.packagesparam-name>
<param-value>com.jaxparam-value>
param>
这个配置其实没啥作用,param-value可以设置为任何字符串,但必需有这个配置,不然报错,建议尽量设置成项目上层的目录,比如这里使用“com.jax”。
2.不能使用“org.glassfish.jersey.servlet.ServletContainer”去创建servlet。
package com.jax.JerseySpring.api;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.jax.JerseySpring.mgr.acct.IAcctMgr;
@Component
@Path("/demo")
public class HelloResources {
@Autowired
public IAcctMgr acctMgr;
@GET
@Path("/hello")
public String acct() {
String result = "failure";
try {
result = acctMgr.getAcctByCd("9527");
System.out.println("结果:" + result);
} catch (Exception e) {
System.out.println(e);
}
return result;
}
}
@Component:是spring里标注组件的注解
@Path:用来为资源类或方法定义URI
@GET:表明被注解的方法响应HTTP GET请求,@POST、@PUT和@DELETE同理
其实Jersey的注解就那几个,数都能数过来。
这个管理类就是server层的类,其实这篇文章的目的是为了在jersey资源里使用spring的注解
接口类IAcctMgr.java:
package com.jax.JerseySpring.mgr.acct;
public interface IAcctMgr {
public String getAcctByCd(String cd);
}
实现类AcctMgrImpl.java:
package com.jax.JerseySpring.mgr.acct.impl;
import org.springframework.stereotype.Service;
import com.jax.JerseySpring.mgr.acct.IAcctMgr;
@Service
public class AcctMgrImpl implements IAcctMgr {
@Override
public String getAcctByCd(String cd) {
return "查询到帐户cd:" + cd + "的信息";
}
}
重点注意一个注解@Service,标识组件,别忘了。
到目前为止,整个demo就完成了,让它在tomcat里跑起来吧。
这里使用tomcat,将JerseySpring发布到tomcat容器里。成功运行没报错的情况下,在浏览器里输入:
http://127.0.0.1:8080/JerseySpring/api/demo/hello
说明与spring成功接入,能使用spring的注解了,标准说法是:能成功的将创建的实例存放在jersey的SpringServlet容器里。
CSDN上传资源不能免费,我也是醉了,这里不好意思了,这里使用百度云:
JerseySpring例子下载
密码: 2itw