首先配置CXF环境,从apache下载cxf所需jar包。加入你的构建路径。
第二,建立一个DTO对象(用于参数是值对象类型),如下:
package bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
*@author 刘岩
*/
@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "name", "age" })
public class Customer {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
因为采用了注解形式,若不是JDK1.5,那你只能望CXF而兴叹了,还是用XFire吧。
接下来定义服务接口
package service;
import javax.jws.WebService;
import bean.Customer;
/**
*@author 刘岩
*/
@WebService(targetNamespace="iscpWebService/RemoveCooperater")
public interface CXFService {
public String sayHello(String inStr);
public void saveCust(Customer customer);
}
下面写接口实现类
package service.impl;
import javax.jws.WebService;
import bean.Customer;
import service.CXFService;
/**
*@author 刘岩
*/
@WebService(targetNamespace = "iscpWebService/RemoveCooperater", endpointInterface = "service.CXFService")
public class CXFServiceImpl implements CXFService {
public String sayHello(String inStr) {
System.out.println("服务端调用");
inStr = "
return inStr;
}
public void saveCust(Customer customer) {
if (customer != null) {
customer.setName(customer.getName()+"@123");
System.out.println(customer.getName() + ":" + customer.getAge());
}
}
}
因为CXF依赖于Spring框架,所以
web.xml修改如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
org.springframework.web.context.ContextLoaderListener
org.apache.cxf.transport.servlet.CXFServlet
而Spring的bean.xml内容如下:
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">
==================================================
好了,之后建立一个客户端项目(纯Java项目即可,无需Java Web支持),加入junit单元测试即可。
写客户端代码如下:
package test;
import junit.framework.TestCase;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client extends TestCase {
public void test01() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(CXFService.class);
factoryBean
.setAddress("http://127.0.0.1:8181/MyCXFDemo/webservice/CXFService");
CXFService client = (CXFService) factoryBean.create();
String response = client.sayHello("liuyan");
System.out.println("Response: " + response);
System.exit(0);
}
public void test02() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(CXFService.class);
factoryBean
.setAddress("http://127.0.0.1:8181/MyCXFDemo/webservice/CXFService");
CXFService client = (CXFService) factoryBean.create();
Customer customer = new Customer();
customer.setAge(25);
customer.setName("liuyan");
client.saveCust(customer);
System.out.println("Response: " + customer.getName());
System.exit(0);
}
}
运行test01、test02成功调用服务端代码。tomcat、weblogic都测试通过。注意:有些情况是在tomcat可以正常生成wsdl,但是移植到了weblogic下面会因为jar冲突导致wsdl不能生成。
wsdl如下,用它也可以生成客户端代码:::
xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="CXFServiceImplService" targetNamespace="iscpWebService/RemoveCooperater" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="iscpWebService/RemoveCooperater" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="iscpWebService/RemoveCooperater" version="1.0" xmlns:tns="iscpWebService/RemoveCooperater" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Customer" type="tns:customer" />
<xs:element name="saveCust" type="tns:saveCust" />
<xs:element name="saveCustResponse" type="tns:saveCustResponse" />
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xs:complexType name="saveCust">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:customer" />
xs:sequence>
xs:complexType>
- <xs:complexType name="customer">
- <xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element name="age" type="xs:int" />
xs:sequence>
xs:complexType>
- <xs:complexType name="saveCustResponse">
<xs:sequence />
xs:complexType>
- <xs:complexType name="sayHello">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
xs:sequence>
xs:complexType>
- <xs:complexType name="sayHelloResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
xs:sequence>
xs:complexType>
xs:schema>
wsdl:types>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
wsdl:message>
- <wsdl:message name="saveCustResponse">
<wsdl:part element="tns:saveCustResponse" name="parameters" />
wsdl:message>
- <wsdl:message name="saveCust">
<wsdl:part element="tns:saveCust" name="parameters" />
wsdl:message>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
wsdl:message>
- <wsdl:portType name="CXFService">
- <wsdl:operation name="saveCust">
<wsdl:input message="tns:saveCust" name="saveCust" />
<wsdl:output message="tns:saveCustResponse" name="saveCustResponse" />
wsdl:operation>
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
wsdl:operation>
wsdl:portType>
- <wsdl:binding name="CXFServiceImplServiceSoapBinding" type="tns:CXFService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="saveCust">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="saveCust">
<soap:body use="literal" />
wsdl:input>
- <wsdl:output name="saveCustResponse">
<soap:body use="literal" />
wsdl:output>
wsdl:operation>
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
<soap:body use="literal" />
wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
wsdl:output>
wsdl:operation>
wsdl:binding>
- <wsdl:service name="CXFServiceImplService">
- <wsdl:port binding="tns:CXFServiceImplServiceSoapBinding" name="CXFServiceImplPort">
<soap:address location="http://127.0.0.1:8181/MyCXFDemo/webservice/CXFService" />
wsdl:port>
wsdl:service>
wsdl:definitions>