本节就CXF框架说明一下对象传递
第一步:创建传输对象Customer
如果JAXB将一个类绑定到了XML,那么默认地,所有的public成员将会被绑定。比如,公共的getter和setter方法对,或者公共的field。任何protected,package-visible或者private的成员都会被绑定,如果它被添加了一个合适的注解,比如@XmlElement
或者@XmlAttribute
。你有若干的可能性来影响它的默认行为。
@XmlRootElement-指定XML根元素名称(可选)
@XmlAccessorType-控制属性或方法序列化
四种方案:
FIELD-对每个非静态(static),非瞬变(transient)属性JAXB工具自动绑定成XML,除非注明XmlTransient
NONE-不做任何处理
PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient
PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注明XmlTransient
@XmlType-映射一个类或一个枚举类型成一个XML Schema类型
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "name", "age" })
public class Customer {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}</span>
第二步:创建WebService接口
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.jws.WebService;
@WebService
public interface HelloService {
public void save(Customer c1, Customer c2);
public void test(String args);
public Customer get(int id);
}</span>
第三步:创建WebService接口实现类
<span style="font-family:KaiTi_GB2312;font-size:18px;">import javax.jws.WebService;
@WebService
public class HelloServiceImpl implements HelloService {
public void save(Customer c1, Customer c2) {
System.out.println(c1.getAge() + "---" + c2.getAge());
System.out.println(c1.getName() + "---" + c2.getName());
}
public void test(String args) {
System.out.println(args);
}
public Customer get(int id) {
Customer cus = new Customer();
cus.setAge(100);
cus.setName("Josen");
return cus;
}
}</span>
第四步:创建服务端
<span style="font-family:KaiTi_GB2312;font-size:18px;">import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class SoapServer {
public static void main(String[] args) {
// 两种方法,任选一种发布WebService接口
// Endpoint.publish("http://localhost:8080/helloService", new
// HelloServiceImpl());
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:8080/helloService");
factory.setServiceClass(HelloServiceImpl.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}</span>
第五步:创建客户端
<span style="font-family:KaiTi_GB2312;font-size:18px;">import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class SoapClient {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
<span style="white-space:pre"> </span>factory.setAddress("http://localhost:8080/helloService");
<span style="white-space:pre"> </span>factory.setServiceClass(HelloService.class);
<span style="white-space:pre"> </span>factory.getInInterceptors().add(new LoggingInInterceptor());
<span style="white-space:pre"> </span>HelloService service = (HelloService) factory.create();
<span style="white-space:pre"> </span>Customer c1 = new Customer();
<span style="white-space:pre"> </span>c1.setAge(1);
<span style="white-space:pre"> </span>c1.setName("aaa");
<span style="white-space:pre"> </span>Customer c2 = new Customer();
<span style="white-space:pre"> </span>c2.setAge(2);
<span style="white-space:pre"> </span>c2.setName("bbb");
<span style="white-space:pre"> </span>service.save(c1, c2);
<span style="white-space:pre"> </span>service.test("aaaaaaaaaaaaa");
<span style="white-space:pre"> </span>}
}</span>
<span style="font-family:KaiTi_GB2312;">最后,测试程序
运行服务端程序,在浏览器地址栏输入http://localhost:8080/helloService?wsdl查看接口是否发布成功。成功则运行一下客户端程序,看看对象传输是否成功。
现在我们来分析一下控制打印的日志信息。
信息: Inbound Message
----------------------------
ID: 1
Address: /HelloWorld
Encoding: UTF-8
Content-Type: text/xml; charset=UTF-8
Headers: {content-type=[text/xml; charset=UTF-8], connection=[keep-alive], Host=[localhost:9000], Content-Length=[184], SOAPAction=[""], User-Agent=[Apache CXF 2.2.2], Content-Type=[text/xml; charset=UTF-8], Accept=[*/*], Pragma=[no-cache], Cache-Control=[no-cache]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:say xmlns:ns1="http://client.itdcl.com/"><text> Josen</text></ns1:say></soap:Body></soap:Envelope>
--------------------------------------
2010-1-9 20:41:56 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
信息: Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><text xmlns="http://client.itdcl.com/">hi Josen</text></soap:Header><soap:Body><ns1:sayResponse xmlns:ns1="http://client.itdcl.com/"></ns1:sayResponse></soap:Body></soap:Envelope>
--------------------------------------
2010-01-09 20:41:56.578::INFO: seeing JVM BUG(s) - cancelling interestOps==0
当客户端向服器发送请求时,服务端LoggingInInterceptor拉截客户端发送过来的SOAP消息,如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:sayHi xmlns:ns1="http://client.itdcl.com/">
<text>Josen</text>
</ns1:sayHi>
</soap:Body>
</soap:Envelope>
客户端将请求信息封闭在<soap:Body></soap:Body>中,当然也可以将其放到<soap:Header></soap:Header>,只要在@WebParam中的header设置成true,默认为false;
服务器接到请求之后,响应客户端。同样以SOAP形式将信息封装好发回客户端,SOAP信息如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<text xmlns="http://client.itdcl.com/">hi Josen</text>
</soap:Header>
<soap:Body>
<ns1:sayResponse xmlns:ns1="http://client.itdcl.com/"></ns1:sayResponse>
</soap:Body>
</soap:Envelope> </span>