搭建jersey+mybits+spring框架

1.首先将需要的依赖加入到本地资源库

<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/maven-v4_0_0.xsd">
 
  <modelVersion>4.0.0modelVersion>
  
  <groupId>com.scgroupId>
  
  <artifactId>cj_smjartifactId>
  
  <packaging>warpackaging>
  
  <version>0.0.1-SNAPSHOTversion>
  
  <name>cj_smj Maven Webappname>
  
  <url>http://maven.apache.orgurl>
  
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring-version>4.1.6.RELEASEspring-version>
        <junit-version>3.8.1junit-version>
        <tomcat7-maven-plugin-version>2.1tomcat7-maven-plugin-version>
        <jersey-version>2.17jersey-version>
    properties>
    

  <dependencies>
  
    <dependency>
        
      <groupId>junitgroupId>
      
      <artifactId>junitartifactId>
      
      <version>${junit-version}version>
      
      <scope>testscope>
    dependency>
    
         <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poiartifactId>
        <version>3.16version>
        dependency>

     

<dependency>  
   <groupId>org.glassfish.jersey.containersgroupId>  
   <artifactId>jersey-container-servletartifactId>  
   <version>${jersey-version}version>  
dependency> 

<dependency>  
   <groupId>org.glassfish.jersey.coregroupId>  
   <artifactId>jersey-clientartifactId>  
   <version>${jersey-version}version>  
dependency>  

<dependency>  
   <groupId>org.glassfish.jersey.mediagroupId>  
   <artifactId>jersey-media-json-jacksonartifactId>   
   <version>${jersey-version}version>  
dependency>  

<dependency> 
   <groupId>org.glassfish.jersey.mediagroupId> 
   <artifactId>jersey-media-moxyartifactId> 
   <version>${jersey-version}version> 
   dependency> 

   
       <dependency>  
            <groupId>javax.servletgroupId>  
            <artifactId>javax.servlet-apiartifactId>  
            <version>3.0.1version>  
            <scope>providedscope>  
        dependency>  

        <dependency>
        <groupId>org.glassfish.jersey.extgroupId>
        <artifactId>jersey-mvc-jspartifactId>
        <version> 2.17 version>
    dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring-version}version>
        dependency>
            <dependency> 
            <groupId>org.glassfish.jersey.extgroupId> 
            <artifactId>jersey-spring3artifactId> 
            <version>${jersey-version}version>


            
            
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>3.0-alpha-1version>
            <type>jartype>
            <scope>providedscope>
        dependency>
        
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        
        <dependency>
        <groupId>commons-dbcpgroupId>
        <artifactId>commons-dbcpartifactId>
          <version>1.4version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>1.2.2version>
     dependency>
     <dependency>
         <groupId>org.mybatisgroupId>
         <artifactId>mybatisartifactId>
         <version>3.2.7version>
     dependency>

     

     
    
    <dependency>  
         <groupId>com.oraclegroupId>
        <artifactId>ojdbc6artifactId>
        <version>11.2.0.1.0version>
     dependency>
    
  dependencies>

  <build>
    <finalName>cj_smjfinalName>
    
    <pluginManagement>
            <plugins>
            
                <plugin>
                    <groupId>org.apache.tomcat.mavengroupId>
                    <artifactId>tomcat7-maven-pluginartifactId>
                    <version>2.1version>
                    
                    <configuration>
                        <uriEncoding>UTF-8uriEncoding>
                        <port>8080port>
                        <path>/path>
                    configuration>
                plugin>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-javadoc-pluginartifactId>
                    <version>2.8.1version>
                plugin>
            plugins>
        pluginManagement>
  build>
project>

2.配置applicationContent.xml也就是配置spring.xml


<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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" >


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
        <property name="username" value="system"/>
        <property name="password" value="admin"/>
        
        <property name="initialSize" value="10"/>
        
        <property name="maxActive" value="100"/>
        
        <property name="maxIdle" value="50"/>
        
        <property name="minIdle" value="10"/>
        
        <property name="validationQuery" value="select 1 from dual"/>
        
        <property name="testOnBorrow" value="true"/>
        <property name="removeAbandonedTimeout" value="120"/>
        <property name="removeAbandoned" value="true"/>
        
        <property name="timeBetweenEvictionRunsMillis" value="3600000"/>
        
        <property name="minEvictableIdleTimeMillis" value="3600000"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:mybatis-config.xml">property>
    bean>

        
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cj.dao">property>
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
     bean>

     
     <context:component-scan base-package="com.cj.service">
    context:component-scan>

     
     <context:component-scan base-package="com.cj">
        <context:exclude-filter type="regex" expression="com.cj.resource" />
     context:component-scan> 

beans>

3.写一个jersey的资源注册类,里面就是转换数据和将你的资源暴露

package com.cj;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
import org.glassfish.jersey.moxy.json.MoxyJsonFeature;
import com.cj.resource.UserController;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class RestApplication extends ResourceConfig{
    public RestApplication(){    
      //加载resource
      register(UserController.class);
      //注册 JSON 转换器   
      register(MoxyJsonFeature.class); 
      register(JacksonJsonProvider.class); 
      register(JspMvcFeature.class);//注册MVC支持 
      //jsp文件所在位置,当前JSP文件是在项目根目录下  
      property(JspMvcFeature.TEMPLATE_BASE_PATH,"/");
     }   
}

4.最后配置web.xml



<web-app>
     
  <display-name>Archetype Created Web Applicationdisplay-name>
      
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
  
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
    listener>
    
   <servlet> 
        <servlet-name>Jersey Web Applicationservlet-name> 
        <servlet-class>org.glassfish.jersey.servlet.ServletContainerservlet-class> 
        <init-param> 
            <param-name>javax.ws.rs.Applicationparam-name> 
            <param-value>com.cj.RestApplicationparam-value> 
        init-param> 
        <load-on-startup>1load-on-startup> 
        <async-supported>trueasync-supported> 
    servlet> 
    
    <servlet-mapping> 
    <servlet-name>Jersey Web Applicationservlet-name> 
        <url-pattern>/cj/*url-pattern> 
    servlet-mapping> 

      <welcome-file-list>
        <welcome-file>/index.jspwelcome-file>
    welcome-file-list>

web-app>

5.最后测试:

//根资源,相当于mvc里面的maaping
@Path("/")
public class UserController {
    @Autowired
    BaseService BaseService
    //表示被标记的方法是什么样的请求
    @GET
    //相当于一个子资源,用来找具体的方法。
    @Path("/index")
    public String index(){
        return "123123";
    }
    }

输入路径http://localhost:8080/cj/index
如果页面上出现123123,搭建成功!

你可能感兴趣的:(搭建jersey+mybits+spring框架)