xfire配备webService的方法步骤

2016-04-28 09:42:20 文章

xfire配置webService的方法步骤

第一步:建一个secn的services.xml文件 META-INF > xfire > services.xmlHelloWorldServiceurn:helloworld:service:xfire:itcast:cncn.itcast.xfire.service.HelloWorldcn.itcast.xfire.service.HelloWorldService第二步:建一个web.xml文件XFireServletXFire Servletorg.codehaus.xfire.transport.http.XFireConfigurableServletXFireServlet/servlet/XFireServlet/*XFireServlet/services/*测试方法一:@Testpublic void testXfire1() throws Exception{Service service = new Service();Call call = (Call) service.createCall();String url = "http://localhost:8080/secn/services/HelloWorldService" ;call.setTargetEndpointAddress(new URL(url));call.setOperationName("sayHello");System.out.println(call.invoke(new Object[]{"tom"}));}测试方法二:这个url地址一定要加上?wsdl并在 new Object[]{"tom"})[0]这里要加上[0]@Testpublic void testXfire2() throws Exception{String url = "http://localhost:8080/secn/services/HelloWorldService?wsdl" ;Client c = new Client(new URL(url));System.out.println(c.invoke("sayHello", new Object[]{"tom"})[0]);}测试方法三:@Testpublic void testXfire3() throws Exception{String url = "http://localhost:8080/secn/services/HelloWorldService" ;ObjectServiceFactory serviceFactory = new ObjectServiceFactory();org.codehaus.xfire.service.Service serviceModel = serviceFactory.create(IHelloWorld.class);XFireProxyFactory proxyFactory = new XFireProxyFactory();IHelloWorld hw = (IHelloWorld)proxyFactory.create(serviceModel,url);System.out.println(hw.sayHello("Tome"));}第三步:cn.com.secn.xfire.service.jsr181.CustomerService#jsr181ServiceFactory

CostomerService类://在这里也可以添服务名@WebServicepublic class CustomerService {private List customers = new ArrayList();public CustomerService() {}//header = true是以头部发送方式@WebMethod@WebResult(name = "Customers") //返回类型public Collection getCustomers(@WebParam(name = "UserToken", header = true)UserToken auth) {authorize(auth);return customers;}private void authorize(UserToken auth) {System.out.println(auth.getUsername());System.out.println(auth.getPassword());}@WebMethodpublic String addCustomer(@WebParam(name = "UserToken", header = true)UserToken auth, @WebParam(name = "customer") Customer customer) {authorize(auth);customers.add(customer);return "tommm";}}

本文地址:https://www.sdfzl.com/jiaocheng-140123-1.shtml

相关阅读

热门