1 首先下载cxf www.apache.org 找到cxf
修改配置文件web.xml 添加下面一段
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
2 写个简单的hellword
package test.com.launch.x431.business;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
//标注是webservice
@WebService
public interface IHelloWorld
{
//WebResult 是返回时wsdl 的显示名称 WebParam里面的name 是参数名称
public @WebResult (name="result")String sayHello(@WebParam(name="text")String text);
}
3 实现
package test.com.launch.x431.business;
import javax.annotation.Resource;
import javax.servlet.http.*;
import javax.xml.rpc.server.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.MessageContext.Scope;
import javax.xml.ws.WebServiceContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
//@WebService(endpointInterface = "test.com.launch.x431.business.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
@Resource
private WebServiceContext context;
public String sayHello(String text) {
System.out.println("sayHello is called!");
String CustomerIP="";
//此处可以取得客户端的ip
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
String ip=request.getRemoteAddr();
System.out.println(request.getRemoteHost());
System.out.println(ip+"----");
return "Hello " + text;
}
}
4 WEB-INFO 下面添加 cxf-servlet.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:server id="jaxwsService"
serviceClass="test.com.launch.x431.business.IHelloWorld"
address="/testHello">
<jaxws:serviceBean>
<bean class="test.com.launch.x431.business.HelloWorldImpl" />
</jaxws:serviceBean>
<!--输入输出拦截器-->
<jaxws:inInterceptors>
<bean
class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean
class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
<beans>
部署后自动生成wsdl 访问下 就可以看到生成的 wsdl 8080 后是项目名称
http://localhost:8080/x431/services/testHello?wsdl
5 客户端测试
package com.launch.test;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.xml.soap.SOAPException;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.launch.service.IHelloWorld;
public class Test {
public static void main(String[] args) throws ParseException,
UnsupportedOperationException, SOAPException {
Map<String, Object> props = new HashMap<String, Object>();
//如果是传文件的时候 打开这个 大文件的时候会快很多
props.put("mtom-enabled", Boolean.TRUE);
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http://localhost:8080/x431/services/testHello");
soapFactoryBean.setServiceClass(IHelloWorld.class);
soapFactoryBean.setProperties(props);
Object o = soapFactoryBean.create();
IHelloWorld helloService = (IHelloWorld) o;
String hello= helloService.sayHello(" world");
System.out.println(hello);
}
}
刚开始做的时候,出现很多问题,很多时候都是因为jar包问题引起的
如 原来我的 cglib-full-2.0.2 出现问题了 换成了 cglib-nodep-2.2.jar
或者少了 FastInfoset-1.2.7.jar
有时候cxf 版本不一样也会出现问题 我用的 是 cxf-2.2.5.jar