cxf jax-rs client 笔记

来源:互联网 发布:杨百万炒股软件 编辑:程序博客网 时间:2024/05/16 17:03

参考:http://cxf.apache.org/docs/jax-rs-client-api.html

第一步:

         根据WS服务端提供的信息 编写一个Service接口

例如:

[java] view plain copy
  1. import javax.ws.rs.POST;  
  2. import javax.ws.rs.Path;  
  3. import com.dotwconnect.us.xsd.getallcountries.Customer;  
  4. import com.dotwconnect.us.xsd.getallcountries_response.ResultType;  
  5. @Path("/gateway.dotw")  
  6. public interface CountriesService {  
  7.   
  8.     @POST  
  9.     ResultType getAllCountries(Customer customer);  
  10.       
  11. }  

第二步:

    通过JAXRSClientFactory 工厂对象创建Service

[java] view plain copy
  1. CountriesService cs = JAXRSClientFactory.create("http://localhost:8888",  
  2.                 CountriesService.class);  
  3.           
  4.  Customer c = new Customer() ;  //请求参数  
  5.   
  6. ResultType xxx = cs.getAllCountries(c);  //访问WS  


注释:

   发送出去的XML消息,如果要去掉namespace 信息 ,可以将生成的客户端对象中的package-info对象中的@XmlSchema注解中的 namespace 属性删掉。

 

如果返回过来的xml消息。节点名称与我们类名不符合。

如:返回过来的消息为

   <result>…</result>

但是我们生成的类名为 ResultType 

那么使用JAXb Unmarshaller 的时候,就有问题 。解决办法就是给ResultType加上一个@XmlRootElement(name="result")注解

 


Spring中配置

在项目中加入jaxrs-https.xml文件,内容如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.            http://www.springframework.org/schema/tx  
  10.            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  13.            http://www.springframework.org/schema/context  
  14.            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  15.            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">  
  16.              
  17.     <jaxrs:client id="countriesClient"    
  18.         serviceClass="com.zf.service.CountriesService"  
  19.         address="http://localhost:8888"  
  20.         inheritHeaders="true">  
  21.         <jaxrs:headers>  
  22.             <entry key="Accept" value="text/xml" />  
  23.         </jaxrs:headers>  
  24.     </jaxrs:client>  
  25.       
  26. </beans>  

然后就可以通过下面的方式获取服务接口了

[java] view plain copy
  1. ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});  
  2.   
  3.         CountriesService countriesService = (CountriesService)cxt.getBean("countriesClient");  
0 0
原创粉丝点击