pom.xml
<!-- 版本属性配置 -->
<properties>
<!-- Main version setting -->
<spring.version>4.1.2.RELEASE</spring.version>
<spring-security.version>4.1.2.RELEASE</spring-security.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.2.3</mybatis-spring.version>
<druid.version>1.0.18</druid.version>
<c3p0.version>0.9.1.2</c3p0.version>
<sitemesh.version>2.4.2</sitemesh.version>
<ehcache.version>2.6.11</ehcache.version>
<ehcache-web.version>2.0.4</ehcache-web.version>
<activiti.version>5.16.3</activiti.version>
<!-- JDBC driver setting -->
<microsoft.driver.version>1.0</microsoft.driver.version>
<mysql.driver.version>5.1.16</mysql.driver.version>
<oracle.driver.version>14.0</oracle.driver.version>
<junit.version>4.11</junit.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-context</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-aop</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring end -->
<!-- Spring orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.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>
<exclusions>
<exclusion>
<artifactId>asm</artifactId>
<groupId>org.ow2.asm</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- AOP end -->
<!-- Dom4j Denpendency Start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- Dom4j Denpendency End -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Test end -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!-- CXF Begin -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.5.0</version>
</dependency>
<!-- CXF End -->
<!-- slf4j Begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- slf4j End -->
</dependencies>
web.xml
<!-- CXF配置 -->
<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>
<!-- webservice的地址就是http://localhost:8080/projectName/webservice/*** -->
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
spring配置文件application.xml
<!-- implementor为接口实现类的bean的id拼接上#,我这里是在接口实现类上用了@Component(value = "medicareServiceImpl"),address为自定义路径 -->
<jaxws:endpoint id="medicareService" implementor="#medicareServiceImpl" address="/medicareService" />
接口 MedicareService.java
import javax.jws.WebService;
@WebService
public interface MedicareService {
public void one();
}
实现类 MedicareServiceImpl.java
import javax.jws.WebService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.maixure.cxfService.MedicareService;
@Component(value = "medicareServiceImpl")
//endpointInterface为接口路径
@WebService(endpointInterface = "com.lwb.cxfService.MedicareService")
public class MedicareServiceImpl implements MedicareService {
private Logger logger = LoggerFactory.getLogger(getClass());
public void one() {
system.out.println("Hello World!");
}
}
访问地址
http://localhost:8080/projectName/webservice/medicareService?wsdl
单元测试,通过JAX_WS调用WebService接口
/**
*此方法中调用的接口并非上述流程所创建的接口,此方法主要演示使用JAX-WS调用接口的流程
*/
@Test
public void four() throws Exception{
String url = "http://127.0.0.1:8080/medicareWebservice/webservice/testService?wsdl";
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(url);
QName name = new QName("http://cxfService.maixure.com/", "hello");
String xmlStr = "<Personal><name>姓名</name><age>48</age><address>地址</address></Personal>";
Object[] objects;
objects = client.invoke(name, xmlStr);
System.out.println(objects[0]);
CommonUtils.printMsg(objects[0].toString(), "response");
}