原创

详细的SSM+CXF跨域跨平台案列


这是一个spring+mybatis+cxf做的接口开发实现跨域跨平台请求调用
参考文档:
参考文章
由于参考文档没给全资料,在这里我给大家补全了,希望能帮助到大家学习接口开发;
首先是pom.xml:

<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>
  <groupId>com.neusoft.icelake</groupId>
  <artifactId>restservice</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>restservice</name>  

    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <java.version>1.7</java.version>  
        <spring.version>3.2.8.RELEASE</spring.version>  
        <servlet-api.version>2.5</servlet-api.version>  
        <slf4j.version>1.7.5</slf4j.version>  
        <cxf.version>2.7.12</cxf.version>  
        <jackson.version>2.1.4</jackson.version>  

        <mybatis.version>3.2.8</mybatis.version>  
        <mybatisspring.version>1.2.2</mybatisspring.version>  
        <druid.version>1.0.11</druid.version>  
    </properties>  

    <dependencies>  
        <!-- spring begin -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</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-context</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc-portlet</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <!-- spring end -->  

        <!-- @Inject begin -->  
        <dependency>  
            <groupId>javax.inject</groupId>  
            <artifactId>javax.inject</artifactId>  
            <version>1</version>  
        </dependency>  
        <!-- @Inject end -->  

        <!-- servlet begin -->  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>servlet-api</artifactId>  
            <version>${servlet-api.version}</version>  
        </dependency>  
        <!-- servlet end -->  

        <!-- cxf begin -->  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxws</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-rs-extension-providers</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.codehaus.jettison</groupId>  
            <artifactId>jettison</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
        <dependency>  
            <groupId>javax.ws.rs</groupId>  
            <artifactId>jsr311-api</artifactId>  
            <version>1.1.1</version>  
        </dependency>  
        <!-- cxf end -->  

        <!-- JSON begin -->  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-core</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-annotations</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.module</groupId>  
            <artifactId>jackson-module-jaxb-annotations</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>com.fasterxml.jackson.jaxrs</groupId>  
            <artifactId>jackson-jaxrs-json-provider</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.0</version>  
        </dependency>  
        <dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-core-asl</artifactId>  
            <version>1.9.0</version>  
        </dependency>  
        <!-- JSON end -->  

        <!-- MyBatis -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>${mybatis.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>${mybatisspring.version}</version>  
        </dependency>  
        <!-- AOP begin -->  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjrt</artifactId>  
            <version>1.7.4</version>  
        </dependency>  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjweaver</artifactId>  
            <version>1.7.4</version>  
        </dependency>  
        <dependency>  
            <groupId>cglib</groupId>  
            <artifactId>cglib</artifactId>  
            <version>3.1</version>  
        </dependency>  
        <!-- AOP end -->        
        <!-- jdbc driver -->  
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>  
        <!-- 整合数据库连接池 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>   
    </dependencies>  

    <build>  
        <plugins>  
            <!-- compiler插件, 设定JDK版本 -->  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.0</version>  
                <configuration>  
                    <source>1.7</source>  
                    <target>1.7</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  

    <!-- PROGRAMER INFO -->  
    <organization>  
        <name>Neusoft</name>  
        <url>http://www.neusoft.com</url>  
    </organization>
</project>

接着是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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>restservice</display-name>  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:/applicationContext-*.xml</param-value>  
    </context-param>  
    <listener>  
        <description>spring listener</description>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/*</url-pattern> 
    </servlet-mapping>  

    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <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>  
    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
        <welcome-file>index.htm</welcome-file>  
        <welcome-file>index.jsp</welcome-file>  
        <welcome-file>default.html</welcome-file>  
        <welcome-file>default.htm</welcome-file>  
        <welcome-file>default.jsp</welcome-file>  
    </welcome-file-list>  
</web-app>

再接着是applicationContext-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  

    <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" />  

    <!-- 3. 数据源dataSource C3P0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}"/>
        <property name="minPoolSize" value="${c3p0.pool.minPoolSize}"/>
        <property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}"/>
        <property name="acquireIncrement" value="${c3p0.pool.acquireIncrement}"/>
    </bean>  

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="mapperLocations" value="classpath:com/neusoft/icelake/modules/standard/mapper/*.xml" />  
    </bean>  
    <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="append*" propagation="REQUIRED" />  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="modify*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="repair" propagation="REQUIRED" />  
            <tx:method name="delAndRepair" propagation="REQUIRED" />  

            <tx:method name="get*" propagation="SUPPORTS" />  
            <tx:method name="find*" propagation="SUPPORTS" />  
            <tx:method name="load*" propagation="SUPPORTS" />  
            <tx:method name="search*" propagation="SUPPORTS" />  
            <tx:method name="datagrid*" propagation="SUPPORTS" />  

            <tx:method name="*" propagation="SUPPORTS" />  
        </tx:attributes>  
    </tx:advice>  
    <aop:config>  
        <aop:pointcut id="transactionPointcut" expression="execution(* com.neusoft.icelake..*impl.*(..))" />  
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />  
    </aop:config>  

</beans>

applicationContext-server.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 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-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">  

    <context:component-scan base-package="com.neusoft.icelake" />  

    <import resource="classpath*:META-INF/cxf/cxf.xml" />  
    <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  

    <jaxrs:server id="restServiceContainer" address="/rest">  
        <jaxrs:providers>  
            <ref bean="jacksonProvider" />  
        </jaxrs:providers>  
        <jaxrs:extensionMappings>  
            <entry key="json" value="application/json" />  
            <entry key="xml" value="application/xml" />  
        </jaxrs:extensionMappings>  

        <jaxrs:serviceBeans>  
            <ref bean="wholeQuantityService" />
        </jaxrs:serviceBeans>  

        <jaxrs:inInterceptors>  
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>  
        </jaxrs:inInterceptors>  
        <jaxrs:outInterceptors>  
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>  
        </jaxrs:outInterceptors>  
    </jaxrs:server>  

    <bean id="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"></bean>  
    <bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider">  
        <property name="mapper" ref="jacksonObjectMapper" />  
    </bean>  

</beans>

mapper文件:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
<mapper namespace="com.neusoft.icelake.modules.standard.mapper.OrgMapper">    
    <resultMap id="OrgResultMap" type="com.neusoft.icelake.modules.standard.entity.Org">  
        <id column="child_id" property="child_id" jdbcType="VARCHAR" />  
        <result column="child_desc" property="child_desc" jdbcType="VARCHAR" />  
        <result column="child_short" property="child_short" jdbcType="VARCHAR" />  
        <result column="parent_id" property="parent_id" jdbcType="VARCHAR" />  
        <result column="parent_desc" property="parent_desc" jdbcType="VARCHAR" />  
        <result column="org_sort" property="org_sort" jdbcType="VARCHAR" />  
    </resultMap>    
    <sql id="Org_Column_List">  
        parent_id, parent_desc, child_id, child_desc, child_short,  
        org_sort  
    </sql>  
    <select id="getOrgAll" resultMap="OrgResultMap">  
        select  
        <include refid="Org_Column_List" />  
        from v_sap_org_node
    </select>  
</mapper>

java代码:

package com.neusoft.icelake.modules.standard.dao.impl;

import com.neusoft.icelake.modules.standard.dao.BaseDao;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;


public abstract class BaseDaoImpl extends SqlSessionDaoSupport implements BaseDao{
    @Autowired
    //mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    private String ns;      //命名空间
    public String getNs() {
        return ns;
    }
    public void setNs(String ns) {
        this.ns = ns;
    }



}




package com.neusoft.icelake.modules.standard.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/** * @Description: 泛型类,基础的DAO接口 * @Author: yanggang */
public interface BaseDao {

}



package com.neusoft.icelake.modules.standard.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.neusoft.icelake.modules.standard.dao.IWholeQuantityDao;
import com.neusoft.icelake.modules.standard.entity.Org;

@Repository
public class IWholeQuantityDaoImpl extends BaseDaoImpl implements IWholeQuantityDao {

    public IWholeQuantityDaoImpl() {
        // TODO Auto-generated constructor stub
        //设置命名空间
        super.setNs("com.neusoft.icelake.modules.standard.mapper.OrgMapper");
    }
    public List<Org> getOrgAll() {
        return super.getSqlSession().selectList(super.getNs()+".getOrgAll");
    }

}




package com.neusoft.icelake.modules.standard.dao;

import java.util.List;
import java.util.Map;

import com.neusoft.icelake.modules.standard.entity.Org;

public interface IWholeQuantityDao extends BaseDao {

    List<Org> getOrgAll();

}




package com.neusoft.icelake.modules.standard.entity;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "SapOrg")
public class Org implements Serializable {

    private static final long serialVersionUID = 7446548875479453298L;  

    private String parent_id;  

    private String parent_desc;  

    private String child_id;  

    private String child_desc;  

    private String child_short;  

    private String org_sort;  

    public String getParent_id() {  
        return parent_id;  
    }  

    public void setParent_id(String parent_id) {  
        this.parent_id = parent_id;  
    }  

    public String getParent_desc() {  
        return parent_desc;  
    }  

    public void setParent_desc(String parent_desc) {  
        this.parent_desc = parent_desc;  
    }  

    public String getChild_id() {  
        return child_id;  
    }  

    public void setChild_id(String child_id) {  
        this.child_id = child_id;  
    }  

    public String getChild_desc() {  
        return child_desc;  
    }  

    public void setChild_desc(String child_desc) {  
        this.child_desc = child_desc;  
    }  

    public String getChild_short() {  
        return child_short;  
    }  

    public void setChild_short(String child_short) {  
        this.child_short = child_short;  
    }  

    public String getOrg_sort() {  
        return org_sort;  
    }  

    public void setOrg_sort(String org_sort) {  
        this.org_sort = org_sort;  
    }  

    @Override  
    public String toString() {  
        return "Org [parent_id=" + parent_id + ", parent_desc=" + parent_desc + ", child_id=" + child_id  
                + ", child_desc=" + child_desc + ", child_short=" + child_short + ", org_sort=" + org_sort  
                + "]";  
    }
}



package com.neusoft.icelake.modules.standard.entity;

import java.util.List;  
import java.util.Map;  

import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement(name = "SapOrgs")  
public class Orgs {  
    private List<Org> list;  

    private Org[] array;  

    private Map<String, Org> map;  

    public List<Org> getList() {  
        return list;  
    }  

    public void setList(List<Org> list) {  
        this.list = list;  
    }  

    public Org[] getArray() {  
        return array;  
    }  

    public void setArray(Org[] array) {  
        this.array = array;  
    }  

    public Map<String, Org> getMap() {  
        return map;  
    }  

    public void setMap(Map<String, Org> map) {  
        this.map = map;  
    }  
}



package com.neusoft.icelake.modules.standard.service;

import javax.ws.rs.GET;  
import javax.ws.rs.POST;
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  


import com.neusoft.icelake.modules.standard.entity.Orgs;  

@Path("/wq")  
public interface IWholeQuantityService {  

    @POST  
    @Path("/orglist")  
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })  
    public Orgs getOrgList();  

}



package com.neusoft.icelake.modules.standard.service.impl;

import java.util.List;  

import javax.annotation.Resource;
import javax.inject.Named;  




import org.springframework.beans.factory.annotation.Autowired;  




import com.neusoft.icelake.modules.standard.dao.IWholeQuantityDao;
import com.neusoft.icelake.modules.standard.entity.Org;  
import com.neusoft.icelake.modules.standard.entity.Orgs;  
import com.neusoft.icelake.modules.standard.service.IWholeQuantityService;  

@Named("wholeQuantityService")  
public class WholeQuantityServiceImpl implements IWholeQuantityService {  
    @Resource
    private IWholeQuantityDao wholeQuantityDao;  

    public IWholeQuantityDao getWholeQuantityDao() {  
        return wholeQuantityDao;  
    }  

    @Autowired  
    public void setWholeQuantityDao(IWholeQuantityDao wholeQuantityDao) {  
        this.wholeQuantityDao = wholeQuantityDao;  
    }  

    public Orgs getOrgList() {  
        Orgs orgs = new Orgs();  
        List<Org> orglist = wholeQuantityDao.getOrgAll();  
        orgs.setList(orglist);  
        return orgs;  
    }  

}

接着新建一个项目试验跨域跨系统请求
就是一个web项目,里面只有一个index.html
代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript"> $(function(){ $(".id").click(function(){ $.ajax({ type: "post", dataType: "json", url: '/restservice/rest/wq/orglist', data:{'_type':'json', 'password':'jjjj'}, success: function (data) { alert(data); $("#target").html(data); alert("服务加载成功"); } }); }); }); </script>
<body>
    <a class="id" >点击</a>
</body>
</html>

效果图:
这里写图片描述
这里写图片描述
这里写图片描述
在test系统下实现跨域跨系统
这里写图片描述
这里写图片描述
这里写图片描述
http://localhost:8080/restservice
这里写图片描述
这里写图片描述
这里写图片描述
请在Linux系统下使用get方式请求实现跨平台:
http://你自己的访问的地址ip:8080/restservice/rest/wq/orglist?_type=json
这里写图片描述
数据库自己随便建一个就行,请根据上面的访问地址进行访问
附上本项目的源码下载地址:
这里写链接内容

如果有需要的大家可以加我的qq群438581190,大家一起开始自学,互相交流,互相学习。
请尊重我努力的成果,转载或者借鉴请注明出处,谢谢!

正文到此结束