最近梳理了一下之前写的restful服务端,由于以前用NetBeans写的,时间久了感觉NetBeans真是KA、卡!!!
现在用eclipse重新梳理一下,现在把整体结构记录一下,快速搭建一个基本的项目服务架构出来。
一、创建一个Dynamic Web Project项目,项目结构如下图:
二、导入所需要的jar包,jar下载地址(https://pan.baidu.com/s/1eSA3zPo)
三、配置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文件
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"> class="com.mchange.v2.c3p0.ComboPooledDataSource"> class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> class="org.springframework.orm.hibernate4.HibernateTransactionManager">
五、配置web.xml文件
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"> classpath:applicationContext.xml
六、配置数据库连接文件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
至此,相关配置已经完成。
由于项目有些配置问题,如果需要完整项目demo,请留言,单独发送分享一下