xfire配置webService的方法步骤
发布时间:2020-12-16 22:51:19 所属栏目:安全 来源:网络整理
导读:第一步:建一个secn的services.xml文件 META-INF xfire services.xml ?xml version="1.0"? beans xmlns="http://xfire.codehaus.org/config/1.0" !-- 这个是xfirer的配置文件 -- service !-- 这个是xfire的名字 -- nameHelloWorldService/name !-- 这个是名字
第一步:建一个secn的services.xml文件 META-INF > xfire > services.xml <?xml version="1.0"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <!-- 这个是xfirer的配置文件 --> <service> <!-- 这个是xfire的名字 --> <name>HelloWorldService</name> <!-- 这个是名字空间 --> <namespace>urn:helloworld:service:xfire:itcast:cn</namespace> <!-- 这个是接口 --> <serviceClass>cn.itcast.xfire.service.HelloWorld</serviceClass> <!-- 这个是实现类 --> <implementationClass>cn.itcast.xfire.service.HelloWorldService</implementationClass> </service> </beans> 第二步:建一个web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>XFireServlet</servlet-name> <display-name>XFire Servlet</display-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <!-- 通过初始化参数改变xfire配置文件的位置 ;如果改改了.那么services.xml就要和web.xml在一起--> <!-- <init-param> <param-name>config</param-name> <param-value>services.xml</param-value> </init-param> --> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/servlet/XFireServlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> 测试方法一: @Test public 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] @Test public 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]); } 测试方法三: @Test public 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")); } 第三步: <!-- jsr181的配置,是对于用注释方式生成webService --> <service> <serviceClass>cn.com.secn.xfire.service.jsr181.CustomerService</serviceClass> <!-- serviceFactory>jsr181</serviceFactory --> <!-- 这里用的是#号引用下面的Bean --> <serviceFactory>#jsr181ServiceFactory</serviceFactory> </service> <bean id="config" class="org.codehaus.xfire.aegis.type.Configuration"> <property name="defaultExtensibleElements" value="false" /> <property name="defaultExtensibleAttributes" value="false" /> <property name="defaultNillable" value="false" /> <property name="defaultMinOccurs" value="1" /> </bean> <bean name="jsr181ServiceFactory" class="org.codehaus.xfire.annotations.AnnotationServiceFactory"> <constructor-arg ref="xfire.transportManager" index="0" /> <constructor-arg ref="config" index="1" type="org.codehaus.xfire.aegis.type.Configuration" /> </bean> CostomerService类: //在这里也可以添服务名 @WebService public class CustomerService { private List<Customer> customers = new ArrayList<Customer>(); public CustomerService() { } //header = true是以头部发送方式 @WebMethod @WebResult(name = "Customers") //返回类型 public Collection<Customer> 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()); } @WebMethod public String addCustomer(@WebParam(name = "UserToken",header = true) UserToken auth,@WebParam(name = "customer") Customer customer) { authorize(auth); customers.add(customer); return "tommm"; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |