前面几节都是讲一些理论知识,现在又用一个例子来说明一下,这一节我们就CXF框架对象传递进行讲解。
第一步:创建传输对象Customer
@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;
}
}
@XmlRootElement-指定XML根元素名称(可选)
@XmlAccessorType-控制属性或方法序列化
四种方案:
FIELD-对每个非静态,非瞬变属性JAXB工具自动绑定成XML,除非注明XmlTransient
NONE-不做任何处理
PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient
PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注
明XmlTransient
@XmlType-映射一个类或一个枚举类型成一个XML Schema类型
第二步:创建WebService接口
@WebService
public interface HelloService {
public void save(Customer c1,Customer c2);
public void test(String args);
public Customer get(int id);
}
每三步:创建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;
}
}
第四步:创建服务端
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();
}
}
第五步:创建客户端
public class SoapClient {
public static void main(String[] args){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8080/helloService");
factory.setServiceClass(HelloService.class);
factory.setServiceClass(HelloServiceImpl.class);
factory.getInInterceptors().add(new LoggingInInterceptor());
HelloService service = (HelloService)factory.create();
Customer c1 = new Customer();
c1.setAge(1);
c1.setName("aaa");
Customer c2 = new Customer();
c2.setAge(2);
c2.setName("bbb");
service.save(c1, c2);
service.test("aaaaaaaaaaaaa");
}
}
最后,测试程序
运行服务端程序,在浏览器地址栏输入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>
分享到:
相关推荐
WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF+Spring+Hibernate 3.WS-Security WebService CXF学习——JAXB剖析
在CXF框架中,学习进阶篇3介绍了如何进行对象传递。在示例中: 1. **创建传输对象**:定义了一个名为`Customer`的Java类,使用了`@XmlRootElement`、`@XmlAccessorType`和`@XmlType`等JAXB注解,以便将对象序列化为...
本篇将深入探讨如何利用CXF实现客户端,以帮助开发者更好地理解和应用这一强大的工具。 **一、CXF简介** Apache CXF是一个全面的Web服务框架,它的主要功能包括服务提供(Service Provider)、服务消费(Service ...
- 学习WS-*规范,如WS-Security(安全)、WS-ReliableMessaging(可靠消息传递)等,以提升Web服务的质量和安全性。 总之,了解并掌握XFire(或CXF)能帮助开发者在Java环境中高效地构建和使用Web服务。通过样例...
【WebService大讲堂之Axis2及其它Web Service资料】 在IT行业中,Web Service是一种通过互联网进行应用程序间交互的标准技术。它允许不同的系统之间共享数据和服务,不受编程语言、操作系统或硬件平台的限制。本...
五、进阶学习 除了基本的Web服务创建,XFire还支持WS-Security、WS-Addressing等高级特性,可以实现更复杂的Web服务需求。同时,它也可以与Spring框架深度整合,提供AOP(面向切面编程)支持,使得Web服务的管理和...
10. **学习资源与进阶** - 学习更多关于Web服务的知识,如RESTful服务、WS-Security、消息传递模式等。 - 关注Apache CXF项目,了解其最新进展和特性,因为Xfire已经合并到该项目中。 通过这个视频教程,开发者...
最后,我们将探讨一些进阶话题,比如Web服务的互操作性(与非Java平台的通信)、服务组合和 choreography,以及如何使用服务级协议(如WS-ReliableMessaging)来确保消息传递的可靠性。 通过这个Java Web Service...