项目结构图:
步骤一: 添加maven 依赖包
<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.cfx.ws</groupId>
<artifactId>ws-cfx-core</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ws-cfx-core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>2.7.1</cxf.version>
<junit.version>4.8.1</junit.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ws-cxf-core</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9000</port>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这里使用maven-jetty插件.
步骤二:编写service接口
package com.ws.cfx.welc;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.ws.cfx.welc.model.WorkInfo;
/*
* ws 接口
*/
@WebService
public interface WelcomeWS {
@WebMethod
@WebResult String welcomeMessage(@WebParam String message);
@WebMethod
@WebResult WorkInfo workInfoNode(Long id);
}
接口实现
package com.ws.cfx.welc.impl;
import java.util.Date;
import com.ws.cfx.welc.WelcomeWS;
import com.ws.cfx.welc.model.WorkInfo;
/**
*
* @author dennisit@163.com
*
*/
public class WelcomeWSImpl implements WelcomeWS{
@Override
public String welcomeMessage(String message) {
return "server success , accpet message values is :" + message;
}
@Override
public WorkInfo workInfoNode(Long id) {
WorkInfo workInfo = new WorkInfo();
workInfo.setId(2L);
workInfo.setWorkname("Node 2");
workInfo.setWorktime(new Date());
return workInfo;
}
}
WS接口中的方法二,依赖自定义Model类,WorkInfo.
package com.ws.cfx.welc.model;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;
public class WorkInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8709189656486577717L;
private Long id;
private String workname;
private Date worktime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWorkname() {
return workname;
}
public void setWorkname(String workname) {
this.workname = workname;
}
public Date getWorktime() {
return worktime;
}
public void setWorktime(Date worktime) {
this.worktime = worktime;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
步骤三: 编写ws服务端
package com.ws.cfx.welc.server;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.ws.cfx.welc.impl.WelcomeWSImpl;
/**
* ws : 服务端
*
* @author dennisit@163.com
*
* 服务端测试: http://localhost:9000/ws/welcome?wsdl
*/
public class WelcomeWSServer {
/**WS服务地址*/
public static final String WS_URL = "http://localhost:9000/ws/welcome";
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(WelcomeWSImpl.class);
factory.setAddress(WelcomeWSServer.WS_URL);
//添加cfx自定义的日志拦截器
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
Server server = factory.create();
server.start();
}
}
启动服务端,访问地址: http://localhost:9000/ws/welcome?wsdl出现xml输出,表示服务端正式服务.实例输出内容如下:
<wsdl:definitions name="WelcomeWSImplService"
targetNamespace="http://impl.welc.cfx.ws.com/">
<wsdl:import location="http://localhost:9000/ws/welcome?wsdl=WelcomeWS.wsdl"
namespace="http://welc.cfx.ws.com/">
</wsdl:import>
<wsdl:binding name="WelcomeWSImplServiceSoapBinding" type="ns1:WelcomeWS">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="welcomeMessage">
<soap:operation soapAction="" style="document" />
<wsdl:input name="welcomeMessage">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="welcomeMessageResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="workInfoNode">
<soap:operation soapAction="" style="document" />
<wsdl:input name="workInfoNode">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="workInfoNodeResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WelcomeWSImplService">
<wsdl:port binding="tns:WelcomeWSImplServiceSoapBinding"
name="WelcomeWSImplPort">
<soap:address location="http://localhost:9000/ws/welcome" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
步骤四: 编写ws客户端,实现服务调用
package com.ws.cfx.welc.client;
import java.net.SocketTimeoutException;
import javax.xml.ws.WebServiceException;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.ws.cfx.welc.WelcomeWS;
import com.ws.cfx.welc.model.WorkInfo;
import com.ws.cfx.welc.server.WelcomeWSServer;
/**
* ws : 服务客户端
*
* @author dennisit@163.com
*
*
*/
public class WelcomeWSClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(WelcomeWSServer.WS_URL);
factory.setServiceClass(WelcomeWS.class);
//客户端添加cfx日志拦截器
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
WelcomeWS welcome = (WelcomeWS) factory.create();
System.out.println(welcome.welcomeMessage("This if first cfx ws ."));
try {
WorkInfo workInfo = welcome.workInfoNode(2L);
System.out.println(workInfo);
} catch(Exception e) {
if (e instanceof WebServiceException
&& e.getCause() instanceof SocketTimeoutException) {
System.err.println("timeout exception.");
} else {
e.printStackTrace();
}
}
}
}
剔除拦截器日志文件之后的输出内容如下:
server success , accpet message values is :This if first cfx ws .
com.ws.cfx.welc.model.WorkInfo@8a2023[id=2,workname=Node 2,worktime=Thu Apr 03 10:30:04 GMT+08:00 2014]