最近在公司做项目,想采用最近比较热的restful来做,思前想后采用了jersey + spring + mybatis + redis来搭建项目,此处留下详细的配置,方便以后查阅,jersey采用2.13版本,与1.x版本差别比较大,主要是容器的差别!spring采用3.2的版本!

1、采用maven搭建项目

pom如下

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jersey.version>2.13</jersey.version>
        <spring.version>3.2.0.RELEASE</spring.version>
        <quartz.version>2.2.1</quartz.version>
        <shiro.version>1.2.2</shiro.version>
        <httpapi.version>4.1.3</httpapi.version>
        <dbcp.version>1.4</dbcp.version>
        <mybatis.version>3.2.6</mybatis.version>
        <junit.version>4.10</junit.version>
        <mybatis.version>3.2.6</mybatis.version>
        <junit.version>4.10</junit.version>
        <spring-mybatis.version>1.2.2</spring-mybatis.version>
        <mysql.version>5.1.30</mysql.version>
        <log4j.version>1.2.17</log4j.version>
        <fastjson.version>1.2.7</fastjson.version>
        <google.json>2.5</google.json>
        <slf4j.log4j12.version>1.6.4</slf4j.log4j12.version>
        <google.zing.version>3.2.0</google.zing.version>
        <javax.annotation.version>1.2</javax.annotation.version>
        <jstl.version>1.2</jstl.version>
        <servletapi.version>2.4</servletapi.version>
        <jspapi.version>2.2</jspapi.version>
        <sitemesh.version>3.0.0</sitemesh.version>
        <fileupload.version>1.3.1</fileupload.version>
        <dom4j.version>1.6.1</dom4j.version>
        <qiniu-java-sdk>7.0.0</qiniu-java-sdk>
    </properties>
    <dependencies>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency><!--用于aop实现,在下一篇博客里面会写到具体用法-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.8.5</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.5</version>
            </dependency>
            <!-- start jersey dependency -->
            <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
                <artifactId>jersey-media-multipart</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.ext</groupId>
                <artifactId>jersey-spring3</artifactId>
                <version>${jersey.version}</version>
                <exclusions>
                    <exclusion><!--排除jersey自带的spring版本,便于自己控制-->
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aop</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>spring-web</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- end jersey dependency -->
            <!-- start spring dependency -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- end spring dependency -->
            <!-- start quartz dependency -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>${quartz.version}</version>
            </dependency>
            <!-- end quartz dependency -->
            <!-- start http dependency -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <!-- end http dependency -->
            <!-- start common dependency -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>${dbcp.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${fileupload.version}</version>
            </dependency>
            <!-- end common dependency -->
            <!-- start mybatis dependency -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${spring-mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- end mybatis dependency -->
            <!-- start redis dependency -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.4.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.5.0</version>
            </dependency>
            <!-- end redis dependency -->
            <!-- start json dependency -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>${google.json}</version>
            </dependency>
            <!-- end json dependency -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- -->
            <dependency>
                <groupId>servletapi</groupId>
                <artifactId>servletapi</artifactId>
                <version>${servletapi.version}</version>
                <scope>provided</scope><!-- 打包的时候不会打进去,因为tomcat带的有 -->
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jspapi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <!-- -->
            <!-- Shiro security -->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-core</artifactId>
                <version>${shiro.version}</version>
            </dependency><!--登录框架-->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-web</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-cas</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-spring</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>  
                <groupId>org.apache.shiro</groupId>  
                <artifactId>shiro-ehcache</artifactId>  
                <version>${shiro.version}</version>  
            </dependency>  
            <dependency>
                <groupId>org.sitemesh</groupId>
                <artifactId>sitemesh</artifactId>
                <version>${sitemesh.version}</version>
            </dependency>
            <!-- 日志文件 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.log4j12.version}</version>
            </dependency>
            <!-- dom4j -->
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>${dom4j.version}</version>
            </dependency>
            <!-- end -->
            <!-- start 七牛依赖包 -->
            <dependency>
                <groupId>com.qiniu</groupId>
                <artifactId>qiniu-java-sdk</artifactId>
                <version>${qiniu-java-sdk}</version>
            </dependency><!--图片上传用到,图片采用的七牛服务器-->
            <!-- end 七牛依赖包 -->
        </dependencies>

2、web.xml配置

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <module-name>api</module-name>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集编码</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:redis.xml</param-value>
  </context-param>
  <servlet>
      <servlet-name>SpringApplication</servlet-name>
      <!--此处与jersey1.x区别较大-->
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.cn.the3ctv.api</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>SpringApplication</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

3、spring 配置

<?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:task="http://www.springframework.org/schema/task"   
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/task   
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop.xsd
"><!--不写具体的版本,他会自动按照spring的版本来-->
    <task:annotation-driven/>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <context:annotation-config/>
    <context:property-placeholder location="file:/tomcat/config/jdbc.properties,file:/tomcat/config/redis.properties" />
    <context:component-scan base-package="com.cn.api" />
</beans>

4、spring-mybatis配置

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${jdbc.initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${jdbc.maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${jdbc.maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${jdbc.minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${jdbc.maxWait}"></property>
        <property name="validationQuery" value="${jdbc.validationQuery}"></property>
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> 
        <!--removeAbandoned: 是否自动回收超时连接-->  
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:com/cn/api/mapper/**/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cn.api.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

5、redis配置

<?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:p="http://www.springframework.org/schema/p" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  

    <property name="maxTotal" value="${redis.pool.maxActive}" />
        <!--最大空闲连接数-->
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <!--初始化连接数-->
    <property name="minIdle" value="${redis.pool.minIdle}"/>
        <!--最大等待时间-->
    <property name="maxWaitMillis" value="${redis.pool.maxWait}" />
        <!--对拿到的connection进行validateObject校验-->
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
        <!--在进行returnObject对返回的connection进行validateObject校验-->
    <property name="testOnReturn" value="${redis.pool.testOnReturn}" />
        <!--定时对线程池中空闲的链接进行validateObject校验-->
    <property name="testWhileIdle" value="false" />
  </bean>  
  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
    p:host-name="${redis.host}" 
    p:port="${redis.port}" 
    p:password="${redis.pass}"  
    p:pool-config-ref="poolConfig"/> 
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="poolConfig"/>
    <constructor-arg index="1" value="${redis.host}"/>
    <constructor-arg index="2" value="${redis.port}" type="int"/>
    <constructor-arg index="3" value="${redis.timeout}" type="int"/>
    <constructor-arg index="4" value="${redis.pass}"/>
  </bean> 
  <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
    <property name="connectionFactory" ref="connectionFactory" />
  </bean>     
  <bean id="redisService" class="com.cn.api.utils.RedisService"></bean>
</beans>        

7、jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.*.*:3306/vproject?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc.username=root
jdbc.password=*
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=3
jdbc.maxWait=60000
jdbc.validationQuery=select 1
jdbc.testOnBorrow=true

8、redis.preperties

redis.host=192.168.*.*
redis.port=6379
redis.pass=*
redis.database=0

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=1000

redis.pool.maxActive=200
redis.pool.maxIdle=50
redis.pool.minIdle=0
redis.pool.maxWait=15000
##向调用者输出链接资源时,是否检测是否有效,如果无效则从连接池中移除,尝试继续获取,默认为false,建议保留默认值
redis.pool.testOnBorrow=false
##向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.
redis.pool.testOnReturn=false
##maxActive: 链接池中最大连接数,默认为8.
##maxIdle: 链接池中最大空闲的连接数,默认为8.
##minIdle: 连接池中最少空闲的连接数,默认为0.
##maxWait: 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时.
##minEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除。
##softMinEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。默认为-1.
##numTestsPerEvictionRun: 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.
##testOnBorrow: 向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值.
##testOnReturn:  向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.
##testWhileIdle:  向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值.
##timeBetweenEvictionRunsMillis:  “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
##whenExhaustedAction: 当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1:
## -> 0 : 抛出异常,
## -> 1 : 阻塞,直到有可用链接资源
## -> 2 : 强制创建新的链接资源

9、RedisService.java

见redis配置搭建博客

这是自己开发过程中的项目搭建,记录一下,方便以后查阅!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐