前言
首先,为什么想选择Jersey做restful服务呢?我个人比较喜欢它的插件化设计,可以方便的注入自己的全局处理逻辑。再一个就是可以生成wadl描述文件,供查询服务方法。所以在学习spring的过程中,特意抽时间做了jersey+spring集成的验证。在本次搭建的过程中,为了在jersey服务中启动spring事务,从网上查阅了不少。先是看到com.sun.jersey例子,后来发现版本比较低,后转过头去看org.glassfish.jersey。吭吭哧哧服务启动了,却又发现无法通过spring容器实例化服务,无法开启事务,拿不到懒加载数据。又查阅资料来回调试,解决版本不匹配的问题,将jersey-spring3升级成jersey-spring4。摸索的过程总是枯燥的,好在总算是一个个问题的排查解决,终究实现了自己想要的功能! 在这里把我自己摸索过程中涉及到的重点位置分享给大家,希望能对大家有所帮助。
一、关于Jersey
Jersey RESTful 框架是开源的RESTful框架, 实现了JAX-RS (JSR 311 & JSR 339) 规范。它扩展了JAX-RS 参考实现, 提供了更多的特性和工具, 可以进一步地简化 RESTful service 和 client 开发。尽管相对年轻,它已经是一个产品级的 RESTful service 和 client 框架。与Struts类似,它同样可以和hibernate,spring框架整合。
jersey1.X使用的是sun的com.sun.jersey
jersey2.X使用的是glassfish的org.glassfish.jersey
二、集成环境说明
Java:1.8.0_152、SpringMVC:5.0.1.RELEASE、Jersey:2.26-b04、开发工具:eclipse、涉及技术点:springmvc,spring-security,spring data jpa,jersey
三、项目结构简介
3.1、simm.spring.web是web层,simm.spring.restapi是jersey服务层
·
3.2、父级spring-web的POM文件中配置spring jar包依赖
<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.0</modelVersion> <parent> <groupId>simm.study</groupId> <artifactId>spring-study</artifactId> <version>1.0.0</version> </parent> <artifactId>spring-web</artifactId> <packaging>pom</packaging> <properties> <springframework.version>5.0.1.RELEASE</springframework.version> <!--下面这两个是springAOP需要用到 --> <aspectjweaver.version>1.9.0.RC2</aspectjweaver.version> <persistence-api.version>1.0.2</persistence-api.version> <hibernate.version>5.2.12.Final</hibernate.version> <json.version>2.9.2</json.version> <security-version>5.0.0.RELEASE</security-version> <logback.version>1.1.1</logback.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${json.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${json.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${json.version}</version> </dependency> <!-- 4)springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <!-- springmvc-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <!--下面两个提供对 AspectJ 的支持,是 springmvc-aspects 所需要依赖的 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <!-- 这个jar包与hibernate持久化功能冲突,去掉 --> <!-- <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>${persistence-api.version}</version> </dependency> --> <!--这个一定要有、不用报错 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>${security-version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${security-version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${security-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.27-incubating</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Logback dependencies --> <!--<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.3.5.jre8-preview</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> <!-- jpa 2.0以上的版本 需要 spring framework 5 以上支持 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.0.1.RELEASE</version> </dependency> </dependencies> <modules> <module>simm.spring.web</module> <module>simm.spring.dao</module> <module>simm.spring.entity</module> <module>simm.spring.service</module> <module>simm.spring.common</module> <module>simm.spring.restapi</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>