WebService CXF学习:对象传递

来源:互联网 发布:vscode 远程编辑 编辑:程序博客网 时间:2024/05/21 23:39

文章摘抄至 http://xuzhfa123.iteye.com/blog/563331

 

第一步:创建传输对象Customer

package pojo.model;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;}}

@XmlRootElement-指定XML根元素名称(可选) 

@XmlAccessorType-控制属性或方法序列化 

四种方案: 

FIELD-对每个非静态,非瞬变属性JAXB工具自动绑定成XML,除非注明XmlTransient 

NONE-不做任何处理 

PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient 

PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注 

明XmlTransient 

@XmlType-映射一个类或一个枚举类型成一个XML Schema类型 

 

 第二步:创建WebService接口 

package pojo.service;import javax.jws.WebService;import pojo.model.Customer;@WebServicepublic interface HelloService {public void save(Customer c1,Customer c2);public void test(String args);public Customer get(int id);}

 

 每三步:创建WebService接口实现类 

package pojo.service;import javax.jws.WebService;import pojo.model.Customer;@WebServicepublic 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;}}

 

第四步:创建服务端 

package pojo.deploy;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import pojo.service.HelloServiceImpl;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();}}

 

第五步:创建客户端 

package pojo.client;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import pojo.model.Customer;import pojo.service.HelloService;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查看接口是否发布成功。成功则运行一下客户端程序,看看对象传输是否成功。 

 

0 0