用Jersey+spring+hibernate构建RESTful服务
Posted chlin7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Jersey+spring+hibernate构建RESTful服务相关的知识,希望对你有一定的参考价值。
最近梳理了一下之前写的restful服务端,由于以前用NetBeans写的,时间久了感觉NetBeans真是KA、卡!!!
现在用eclipse重新梳理一下,现在把整体结构记录一下,快速搭建一个基本的项目服务架构出来。
一、创建一个Dynamic Web Project项目,项目结构如下图:
二、导入所需要的jar包
三、配置RestApplication.java文件
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;
public class RestApplication extends ResourceConfig {
public RestApplication() {
// 服务类所在的包路径
packages("resources包路径");
// 注册JSON转换器
register(JacksonJsonProvider.class);
}
}
四、配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.zh.rest.service" />
<context:component-scan base-package="com.zh.rest.dao" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="c3p0DataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.pool.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}" />
<property name="maxIdleTime" value="${c3p0.pool.maxIdleTime}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource" />
<property name="packagesToScan">
<list>
<value>com.zh.rest.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bizMethods" expression="execution(* com.zh.rest.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
</beans>
五、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>AHRESTful</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<module-name>AHRESTful</module-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Way REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.zh.rest.RestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Way REST Service</servlet-name>
<url-pattern>/app/rest/*</url-pattern>
</servlet-mapping>
</web-app>
六、配置数据库连接文件jdbc.properties
driverClass = com.microsoft.sqlserver.jdbc.SQLServerDriver
url = jdbc:sqlserver://数据库地址:数据库端口;databaseName=RestDemo
username = 数据库登录名
password =数据库密码
hibernate.dialect = com.zh.rest.util.SqlServer2008Dialect
hibernate.hbm2ddl.auto = true
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = validate
c3p0.pool.maxPoolSize=30
c3p0.pool.minPoolSize=3
c3p0.pool.initialPoolSize=5
c3p0.pool.acquireIncrement=3
c3p0.pool.automaticTestTable=C3P0TestTable
c3p0.pool.testConnectionOnCheckin=true
c3p0.pool.idleConnectionTestPeriod=18000
c3p0.pool.maxIdleTime=25000
c3p0.pool.testConnectionOnCheckout=true
c3p0.pool.autoCommitOnClose=false
至此,相关配置已经完成。
以上是关于用Jersey+spring+hibernate构建RESTful服务的主要内容,如果未能解决你的问题,请参考以下文章
Spring+Jersey+JPA+Hibernate+MySQL实现CRUD操作案例
用Jersey构建RESTful服务5--Jersey+MySQL5.6+Hibernate4.3
从 Migrate 迁移到 Spring MVC 4 + Hibernate5
Hibernate + Jersey + Jackson 随机获得“org.hibernate.TransactionException:不支持嵌套事务”