《WebService CXF学习(进阶篇3):对象传递.pdf》由会员分享,可在线阅读,更多相关《WebService CXF学习(进阶篇3):对象传递.pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、WebService CXF 学习(进阶篇 3):对象传递前面几节都是讲一些理论知识,现在又用一个例子来说明一下,这一节我们就 CXF 框架对象传递进行讲解。第一步:创建传输对象 CustomerJava 代码XmlRootElement(name=Customer)XmlAccessorType(XmlAccessType.FIELD)XmlType(propOrder=name,age)public classCustomer private int age;private String name;public int getAge()return age;public voidsetAg
2、e(int age)this.age=age;public StringgetName()return name;public voidsetName(String name)this.name=name;XmlRootElement-指定 XML 根元素名称(可选)XmlAccessorType-控制属性或方法序列化四种方案:FIELD-对每个非静态,非瞬变属性 JAXB 工具自动绑定成 XML,除非注明 XmlTransientNONE-不做任何处理PROPERTY-对具有 set/get 方法的属性进行绑定,除非注明 XmlTransientPUBLIC_MEMBER-对有 set/ge
3、t 方法的属性或具有共公访问权限的属性进行绑定,除非注明 XmlTransientXmlType-映射一个类或一个枚举类型成一个 XMLSchema 类型第二步:创建 WebService 接口Java代码WebServicepublic interface HelloServicepublic void save(Customer c1,Customer c2);public void test(String args);public Customer get(intid);每三步:创建 WebService 接口实现类Java 代码WebServicepublic classHelloSe
4、rviceImpl implements HelloService publicvoid 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);publicCustomer get(int id)Customer cus=newCustomer();cus.setAge(100);cus.setName
5、(Josen);return cus;第四步:创建服务端Java代码public class SoapServerublic static void main(String args)/两种方法,任选一种发布 WebService 接口/Endpoint.publish(http:/localhost:8080/helloService,newHelloServiceImpl();JaxWsServerFactoryBean factory=newJaxWsServerFactoryBean();factory.setAddress(http:/localhost:8080/helloServ
6、ice);factory.setServiceClass(HelloServiceImpl.class);factory.getInInterceptors().add(newLoggingInInterceptor();factory.getOutInterceptors().add(newLoggingOutInterceptor();factory.create();第五步:创建客户端Java 代码public class SoapClientpublic static void main(Stringargs)JaxWsProxyFactoryBean factory=newJaxWs
7、ProxyFactoryBean();factory.setAddress(http:/localhost:8080/helloService);factory.setServiceClass(HelloService.class);factory.setServiceClass(HelloServiceImpl.class);factory.getInInterceptors().add(newLoggingInInterceptor();HelloService service=(HelloService)factory.create();Customer c1=new Customer(
8、);c1.setAge(1);c1.setName(aaa);Customer c2=newCustomer();c2.setAge(2);c2.setName(bbb);service.save(c1,c2);service.test(aaaaaaaaaaaaa);最后,测试程序运行服务端程序,在浏览器地址栏输入http:/localhost:8080/helloService?wsdl 查看接口是否发布成功。成功则运行一下客户端程序,看看对象传输是否成功。现在我们来分析一下控制打印的日志信息。引用信息:Inbound Message-ID:1Address:/HelloWorldEncod
9、ing:UTF-8Content-Type:text/xml;charset=UTF-8Headers: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-cachePayload:<soap:Envelopex
10、mlns:soap=http:/schemas.xmlsoap.org/soap/envelope/><soap:Body><ns1:sayxmlns:ns1=http:/ 20:41:56org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose信息:Outbound Message-ID:1Encoding:UTF-8Content-Type:text/xmlHeaders:Payload:<soap:Envelopexmlns:soap=http:/schemas.xmls
11、oap.org/soap/envelope/><soap:Header><textxmlns=http:/ 20:41:56.578:INFO:seeing JVM BUG(s)-cancelling interestOps=0当客户端向服器发送请求时,服务端LoggingInInterceptor 拉截客户端发送过来的 SOAP 消息,如下:引用<soap:Envelopexmlns:soap=http:/schemas.xmlsoap.org/soap/envelope/><soap:Body><ns1:sayHi xmlns:ns1=http:/ 中的 header 设置成 true,默认为 false;服务器接到请求之后,响应客户端。同样以 SOAP 形式将信息封装好发回客户端,SOAP 信息如下:引用<soap:Envelopexmlns:soap=http:/schemas.xmlsoap.org/soap/envelope/><soap:Header><text xmlns=http:/