最近在做CXF类型的项目 ,把最近项目中的架构分享给大家.
1.基础环境
JDK1.8+maven+nexus
2.软件
IDEA
3.目录结构:
其余的基本就是SSM的内容,除开webservice包 项目中的webService接口都会在这个包中发布
,webservice为父接口,下面的impl包为具体实现类
4.POM
<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.0</modelVersion>
<groupId>com.pref.webservice</groupId>
<artifactId>webservice</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>webservice Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- Spring 版本 -->
<spring.version>4.3.9.RELEASE</spring.version>
<!-- jackson 版本 -->
<json.version>2.8.7</json.version>
<!-- cxf 版本 -->
<cxf.version>3.1.11</cxf.version>
<!-- mybaties 版本-->
<mybatis.version>3.4.4</mybatis.version>
<!-- 数据库链接池 -->
<druid.version>1.0.31</druid.version>
</properties>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- aspectj start -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- aspectj end -->
<!-- jackson start -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${json.version}</version>
</dependency>
<!-- jackson end -->
<!-- cxf start -->
<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-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- cxf end -->
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<version>1.1.3</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 最新版本为 1.1.0 但是到目前阿里私服上没有更新最新版本 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
<build>
<finalName>webservice</finalName>
<!-- 配置 tomcat插件启动-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8081</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
这里使用了阿里云的链接池[以前没用用过 具体内容参看这里https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 ]
5.配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>webService</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-cxf-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cxf_app</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 加载配置需要和Spring容器一起启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf_app</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
6.配置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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 启动注解 -->
<context:annotation-config/>
<!-- 配置扫描 -->
<context:component-scan base-package="com.pref"/>
<!-- 读取配置文件信息 -->
<context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder>
<!-- 数据库链接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
</bean>
<!-- 创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- *Mapper.xml中的 <mapper namespace="com.pref.dao.UserDao" > namespace 要和对应dao的接口全路径保持一直 -->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.pref.dao"/>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Spring aop事务管理 -->
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.pref.service.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
7.配置Spring-cxf-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--
发布服务
和使用endpoint发布服务类似
WebService地址=tomcat地址+cxf+/weather
-->
<jaxws:server address="/testWeb" serviceClass="com.pref.webservice.TestWebService">
<jaxws:serviceBean>
<!-- 项目中使用@Component 注解来完成IOC容器的装配 -->
<ref bean="testWebService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
8.java代码
接口
package com.pref.webservice;
import com.pref.bean.User;
import javax.jws.WebService;
import java.util.Map;
/**
* Created by Administrator on 2017/6/29/029.
*/
@WebService
public interface TestWebService {
String show(String name);
/**
* 登录服务
* @param name
* @param pwd
* @return
*/
String login(String name, String pwd);
}
实现类
package com.pref.webservice.impl;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.pref.bean.User;
import com.pref.webservice.TestWebService;
import com.pref.webservice.util.XMLUtil;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/6/29/029.
*/
@Component("testWebService")
public class TestWebServiceImpl implements TestWebService {
@Override
public String show(String name) {
System.out.println("服务器发生调用");
return "String name";
}
@Override
public String login(String name, String pwd) {
User user = new User();
user.setCreateTime(new Date());
user.setUserPwd("1231");
user.setUserName("okdd");
user.setName("ddd");
String xml = XMLUtil.obj2Xml(User.class,user);
return xml;
}
}
9.Spring-mvc.xml可以自由发挥,这里就不啰嗦了,我们启动项目访问
http://localhost:8081/ws/testWeb?wsdl 这个地址返回得到
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.pref.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestWebServiceService" targetNamespace="http://webservice.pref.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.pref.com/" elementFormDefault="unqualified" targetNamespace="http://webservice.pref.com/" version="1.0">
<xs:element name="login" type="tns:login"/>
<xs:element name="loginResponse" type="tns:loginResponse"/>
<xs:element name="show" type="tns:show"/>
<xs:element name="showResponse" type="tns:showResponse"/>
<xs:complexType name="show">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="showResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="login">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
<xs:element minOccurs="0" name="arg1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="loginResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="showResponse">
<wsdl:part element="tns:showResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="loginResponse">
<wsdl:part element="tns:loginResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="show">
<wsdl:part element="tns:show" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="login">
<wsdl:part element="tns:login" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="TestWebService">
<wsdl:operation name="show">
<wsdl:input message="tns:show" name="show"></wsdl:input>
<wsdl:output message="tns:showResponse" name="showResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="login">
<wsdl:input message="tns:login" name="login"></wsdl:input>
<wsdl:output message="tns:loginResponse" name="loginResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestWebServiceServiceSoapBinding" type="tns:TestWebService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="show">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="show">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="showResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="login">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="login">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="loginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestWebServiceService">
<wsdl:port binding="tns:TestWebServiceServiceSoapBinding" name="TestWebServicePort">
<soap:address location="http://localhost:8081/ws/testWeb"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
webService接口发布成功
下载:http://download.youkuaiyun.com/detail/pref_mail/9889306