Web Service笔记(五):CXF开发RESTful风格的Web Service

来源:互联网 发布:sql2008新建数据库 编辑:程序博客网 时间:2024/06/06 19:39

前言:

1、Web Service笔记(五):利用CXF结合Spring开发web service

2、XML学习笔记(三):Jaxb负责xml与javaBean映射 

3、jax-rs详解

4、可以使用浏览器的工具调试:如 Firefox 的RESTClient 和chrome的REST Console。


一、配置Spring的配置文件


1、需要引入新的 jar 包。


2、配置 applicationContext-server.xml 文件。使用 jaxrs:server ,记得引入jaxrs 的schema 约束。

1)address:为地址,如 http://localhost:8080/Java_WS_Server/rest/

2)serviceBeans:暴露的ws服务类。也可以使用“#beanId”,引入。

[html] view plain copy
  1. <!-- REST WebService 接口-->  
  2.     <jaxrs:server id="restfulServer" address="/rest">  
  3.         <jaxrs:inInterceptors>  
  4.         </jaxrs:inInterceptors>  
  5.     <jaxrs:serviceBeans>  
  6.         <bean class="cn.rest.rest.SurpolicyEntrence"></bean>      
  7.     </jaxrs:serviceBeans>  
  8.     <jaxrs:extensionMappings>  
  9.         <entry key="json" value="application/json" />  
  10.         <entry key="xml" value="application/xml" />  
  11.     </jaxrs:extensionMappings>  
  12.     <jaxrs:languageMappings>  
  13.            <entry key="en" value="en-gb"/>    
  14.     </jaxrs:languageMappings>  
  15. </jaxrs:server>  



二、Restful 服务类需要实现jax_rs规范。就像ws的服务类需要实现jax_ws规范一样。

1、在 javax.ws.rs.* 中定义,都是一些注解,是 JAX-RS (JSR 311) 规范的一部分。 

2、具体的注解如下:

1)@Path:定义资源基 URI。由上下文根和主机名组成,如:

[html] view plain copy
  1. http://localhost:8080/Java_WS_Server/rest/surpolicy  

2)@GET/@POST:这意味着以下方法可以响应 HTTP GET 或是 HTTP POST方法。 

3)@Produces:响应内容 MIME 类型。如

[java] view plain copy
  1. @Produces(MediaType.TEXT_PLAIN)  
  2. @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  

4)@Context: 使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext 等。

[java] view plain copy
  1. @Context HttpServletRequest servletRequest  
  2. @Context HttpServletResponse servletResponse  
  3.  @Context  
  4.     private UriInfo uriInfo;  

5)@PathParam("contact"):该注释将参数注入方法参数的路径。其他可用的注释有 @FormParam、@QueryParam 等。 


3、一般来说,服务类与方法上应该分别标注注解 @Path,用于区分访问路径。

[java] view plain copy
  1. @Path(value = "/surpolicy")  
  2. public class SurpolicyEntrence {}  
  3. @Path("/sendXml")  
  4. public String sendXml(String requestXML) {}  


三、简单的 RESTful 服务


1、服务类代码:

[java] view plain copy
  1. /** 
  2.  * 简单服务方法 
  3.  * @param input 
  4.  * 访问地址:http://localhost:8080/Java_WS_Server/rest/surpolicy/sendString/queryParams_aa 
  5.  * @return 
  6.  */  
  7. @GET  
  8. @Path("/sendString/{input}")  
  9. // @Produces("text/plain")  
  10. @Produces(MediaType.TEXT_PLAIN)  
  11. public String sendStringParam(@PathParam("input") String input) {  
  12.     System.out.println("接收的参数: \r\n" + input);  
  13.     String tReturn = "成功返回";  
  14.     return tReturn;  
  15.   
  16. }  

启动服务后,访问 http://localhost:8080/Java_WS_Server,有Available RESTful services的内容,说明发布成功。



2、分析:

1)前提:服务类的path定义为如下,故所有的方法的访问地址都为

 http://localhost:8080/Java_WS_Server/rest/surpolicy/ + "方法自己的地址"。/rest 为在配置文件中配置的address地址

[java] view plain copy
  1. @Path(value = "/surpolicy")  
  2. public class SurpolicyEntrence {}  



2)@Path("/sendString/{input}") :用浏览器的rest 工具访问的时候,必须把参数放在url地址后面。如:

[java] view plain copy
  1. http://localhost:8080/Java_WS_Server/rest/surpolicy/sendString/queryParams_aa  

3)访问的结果如下:



后台显示:示成功。返回的信息可以在 RestClient 中查看。

[java] view plain copy
  1. 接收的参数:   
  2. queryParams_aa  


3、客户端代码:本文中的客户端统一使用 org.apache.cxf.jaxrs.client.WebClient 实现。

其他实现方式见:

HTTP访问的两种方式(HttpClient和HttpURLConnection)

利用HttpURLConnection和WebClient发布REST风格的WebService客户端(解决超时问题)

1)WebClient 可以使用spring注入,也可以手工创建:

[java] view plain copy
  1. // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址  
  2. // String url = "http://localhost:8080/Java_WS_Server/rest/";  
  3. // client = WebClient.create(url);  
  4.   
  5. // 从Spring Ioc容器中拿webClient对象,或者直接用注入  
  6. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  7. client = ctx.getBean("webClient", WebClient.class);  


使用spring,需要配置 applicationContext-client.xml 代码:
[html] view plain copy
  1. <bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">  
  2.        <constructor-arg type="java.lang.String" value="http://localhost:8080/Java_WS_Server/rest/" />  
  3.    </bean>  

2)先初始化 webClient对象
[java] view plain copy
  1. public void init() {  
  2.     // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址  
  3.     // String url = "http://localhost:8080/Java_WS_Server/rest/";  
  4.     // client = WebClient.create(url);  
  5.   
  6.     // 从Spring Ioc容器中拿webClient对象,或者直接用注入  
  7.     ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  8.     client = ctx.getBean("webClient", WebClient.class);  
  9.   
  10. }  

3)简单服务的访问方法:

[java] view plain copy
  1. /** 
  2.  * 测试服务端的简单方法 
  3.  */  
  4. public void sendString(){  
  5.     String tResponseMsg = client.path("surpolicy/ping/{input}","我来ping一下。。").accept(MediaType.TEXT_PLAIN).get(String.class);  
  6.     System.out.println(tResponseMsg);  
  7. }  

4)显示:

[java] view plain copy
  1. 服务端:  
  2. 接收的参数:   
  3. 我来ping一下。。  
  4.   
  5. 客户端:  
  6. 成功返回  


四、以XML为交互内容的 RESTful 服务


(一)需要使用 jaxb 来映射Xml与javaBean。

1、接收的javaBean 代码。

[java] view plain copy
  1. package cn.rest.bean;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.xml.bind.annotation.XmlElement;  
  6. import javax.xml.bind.annotation.XmlElementWrapper;  
  7. import javax.xml.bind.annotation.XmlRootElement;  
  8.   
  9. /** 
  10.  *  
  11.  * UserBean.java 
  12.  * 
  13.  * @title User的传输数据类 
  14.  * @description 
  15.  * @author SAM-SHO  
  16.  * @Date 2014-11-25 
  17.  */  
  18.   
  19. @XmlRootElement(name = "USER")  
  20. public class UserBean {  
  21.     private String name;  
  22.     private String age;  
  23.     private UserAddress userAddress;//地址  
  24.     private List<UserPhone> phoneList ;//手机  
  25.   
  26.     @XmlElement(name="NAME")  
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.   
  31.     public void setName(String name) {  
  32.         this.name = name;  
  33.     }  
  34.   
  35.     @XmlElement(name = "AGE")  
  36.     public String getAge() {  
  37.         return age;  
  38.     }  
  39.   
  40.     public void setAge(String age) {  
  41.         this.age = age;  
  42.     }  
  43.   
  44.     @XmlElement(name = "UserAddress")  
  45.     public UserAddress getUserAddress() {  
  46.         return userAddress;  
  47.     }  
  48.   
  49.     public void setUserAddress(UserAddress userAddress) {  
  50.         this.userAddress = userAddress;  
  51.     }  
  52.   
  53.     @XmlElementWrapper(name = "PhoneList")  
  54.     @XmlElement(name = "UserPhone")  
  55.     public List<UserPhone> getPhoneList() {  
  56.         return phoneList;  
  57.     }  
  58.   
  59.     public void setPhoneList(List<UserPhone> phoneList) {  
  60.         this.phoneList = phoneList;  
  61.     }  
  62.       
  63. }  
[java] view plain copy
  1. package cn.rest.bean;  
  2.   
  3. public class UserPhone {  
  4.       
  5.     private String type;//电话号码类型  
  6.     private String num;//电话号码  
  7.       
  8.     public String getType() {  
  9.         return type;  
  10.     }  
  11.     public void setType(String type) {  
  12.         this.type = type;  
  13.     }  
  14.     public String getNum() {  
  15.         return num;  
  16.     }  
  17.     public void setNum(String num) {  
  18.         this.num = num;  
  19.     }  
  20.       
  21. }  

[java] view plain copy
  1. package cn.rest.bean;  
  2.   
  3. import javax.xml.bind.annotation.XmlElement;  
  4.   
  5. public class UserAddress {  
  6.       
  7.     private String homeAddress;//家庭地址  
  8.     private String workAddress;//公司地址  
  9.       
  10.     @XmlElement(name = "HomeAddress")  
  11.     public String getHomeAddress() {  
  12.         return homeAddress;  
  13.     }  
  14.     public void setHomeAddress(String homeAddress) {  
  15.         this.homeAddress = homeAddress;  
  16.     }  
  17.   
  18.     @XmlElement(name = "WorkAddress")  
  19.     public String getWorkAddress() {  
  20.         return workAddress;  
  21.     }  
  22.     public void setWorkAddress(String workAddress) {  
  23.         this.workAddress = workAddress;  
  24.     }  
  25.   
  26. }  


2、返回的javaBean 代码。

[java] view plain copy
  1. package cn.rest.bean.response;  
  2.   
  3. import javax.xml.bind.annotation.XmlElement;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5. /** 
  6.  * 返回商城退保结果对象 
  7.  * @author SAM 
  8.  * 
  9.  */  
  10. @XmlRootElement(name = "RETURN")  
  11. public class ReturnDTO  {  
  12.       
  13.     protected String code;  
  14.     protected String msg;  
  15.       
  16.     @XmlElement(name="Code")  
  17.     public String getCode() {  
  18.         return code;  
  19.     }  
  20.     public void setCode(String code) {  
  21.         this.code = code;  
  22.     }  
  23.     @XmlElement(name="MSG")  
  24.     public String getMsg() {  
  25.         return msg;  
  26.     }  
  27.     public void setMsg(String msg) {  
  28.         this.msg = msg;  
  29.     }  
  30.   
  31. }  

3、转换工具类。

[java] view plain copy
  1. package cn.rest.util;  
  2. import java.io.StringReader;  
  3. import javax.xml.bind.JAXBContext;  
  4. import javax.xml.bind.JAXBException;  
  5. import javax.xml.bind.Marshaller;  
  6. import javax.xml.bind.Unmarshaller;  
  7. import org.xml.sax.InputSource;  
  8. import cn.rest.bean.UserBean;  
  9.   
  10. /** 
  11.  *  
  12.  * ObjectAndXmlHandle.java 
  13.  * 
  14.  * @title jaxb处理xml解析 
  15.  * @description 
  16.  * @author SAM-SHO  
  17.  * @Date 2014-11-25 
  18.  */  
  19. public class ObjectAndXmlHandle {  
  20.       
  21.     public static UserBean parseXml2OUserBean(String xml) {  
  22.         try {  
  23.             JAXBContext context = JAXBContext.newInstance(UserBean.class);  
  24.             InputSource is = new InputSource();  
  25.             StringReader xmlStr = new StringReader(xml);  
  26.             is.setCharacterStream(xmlStr);  
  27.             Unmarshaller unmarshaller = context.createUnmarshaller();  
  28.             UserBean user = (UserBean) unmarshaller.unmarshal(is);  
  29.             return user;  
  30.         } catch (JAXBException e) {  
  31.             e.printStackTrace();  
  32.             return null;  
  33.         }  
  34.     }  
  35.       
  36.     public static void Object2Xml(Object object) {  
  37. //      FileWriter writer = null;         
  38.         try {    
  39.             JAXBContext context = JAXBContext.newInstance(object.getClass());  
  40.             Marshaller marshal = context.createMarshaller();    
  41.             marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);    
  42.             marshal.setProperty("jaxb.encoding""utf-8");  
  43.             marshal.marshal(object, System.out);    
  44.                 
  45. //          writer = new FileWriter("shop.xml");    
  46. //          marshal.marshal(object, writer);    
  47.         } catch (Exception e) {    
  48.             e.printStackTrace();    
  49.         }     
  50.     }  
  51. }  

(二)、服务端方法代码

1、把 xml 以 String 的方法传输。

[java] view plain copy
  1. /** 
  2.  * 接受XML ,推荐使用。 
  3.  * 地址:http://localhost:8080/Java_WS_Server/rest/surpolicy/sendXml  
  4.  * 设置 Content-Type: APPLICATION/XML(可以不设)  
  5.  * body 中设置 xml内容 
  6.  */  
  7. @POST  
  8. @Path("/sendXml")  
  9. @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  10. public String sendXml(String requestXML) {  
  11.     System.out.println("接收的参数:\r\n " + requestXML);  
  12.       
  13.     UserBean tUserBean = ObjectAndXmlHandle.parseXml2OUserBean(requestXML);  
  14.       
  15.     String tReturn = tUserBean.getName()+ " 你好,你的请求成功返回";  
  16.     return tReturn;  
  17. }  

2、RESTClient 工具访问,xml放在 Body 中,可以不设置 Content-Type



3、客户端访问代码:

[java] view plain copy
  1. /** 
  2.  * 发送XML报文 
  3.  */  
  4. private void sendRequestXml() {  
  5.     String tRequestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><USER><AGE>27</AGE><NAME>SAM-SHO</NAME><PhoneList><UserPhone><num>13612345678</num><type>移动</type></UserPhone><UserPhone><num>13798765432</num><type>联通</type></UserPhone></PhoneList><UserAddress><homeAddress>苏州高新区</homeAddress><workAddress>苏州园区</workAddress></UserAddress></USER>";  
  6.     String tResponseMsg = client.path("surpolicy/sendXml").accept(MediaType.APPLICATION_XML).post(tRequestXml, String.class);  
  7.     System.out.println("返回的信息: \r\n" + tResponseMsg);  
  8. }  



五、以JavaBean为交互内容的 RESTful 服务

1、服务端方法代码

[java] view plain copy
  1. /** 
  2.  *接收Bean 
  3.  *  
  4.  * @param user 
  5.  * http://localhost:8080/Java_WS_Server/rest/surpolicy/sendBean 
  6.  * 需要设置 Content-Type: application/xml 
  7.  * body 中设置 xml内容 
  8.  * @return 
  9.  */  
  10. @POST  
  11. @Path("/sendBean")  
  12. @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  13. public ReturnDTO sendBean(UserBean user) {  
  14.   
  15.     //转成报文  
  16.     ObjectAndXmlHandle.Object2Xml(user);  
  17.     System.out.println(user.getUserAddress().getHomeAddress());  
  18.       
  19.     ReturnDTO tReturnDTO = new ReturnDTO();  
  20.     tReturnDTO.setCode("1");  
  21.     tReturnDTO.setMsg(user.getName()+ " ,请求成功,已返回");  
  22.       
  23.     return tReturnDTO;  
  24. }  


2、RESTClient访问

1)一定要设置 headers信息,设置 Content-Type: application/xml ,不然访问不了。



2)需要设置 Content-Type。


3)正确访问:



5)成功返回:



3、客户端访问:

[java] view plain copy
  1. /** 
  2.  * 发送Bean 
  3.  */  
  4. private void sendRequestBean() {  
  5.     UserBean tUserBean = new UserBean();  
  6.     tUserBean.setName("SAM-SHO");  
  7.     tUserBean.setAge("27");  
  8.       
  9.     UserAddress tUserAddress = new UserAddress();  
  10.     tUserAddress.setWorkAddress("苏州园区");  
  11.     tUserAddress.setHomeAddress("苏州高新区");  
  12.     tUserBean.setUserAddress(tUserAddress);  
  13.       
  14.     List<UserPhone> phoneList = new ArrayList<UserPhone>();  
  15.     UserPhone tUserPhone =  new UserPhone();  
  16.     tUserPhone.setType("移动");  
  17.     tUserPhone.setNum("13612345678");  
  18.     phoneList.add(tUserPhone);  
  19.       
  20.     tUserPhone =  new UserPhone();  
  21.     tUserPhone.setType("联通");  
  22.     tUserPhone.setNum("13798765432");  
  23.     phoneList.add(tUserPhone);  
  24.     tUserBean.setPhoneList(phoneList);  
  25.       
  26.     ClientConfiguration config = WebClient.getConfig(client);  
  27.     config.getHttpConduit().getClient().setReceiveTimeout(90000);//设置超时  
  28.     ReturnDTO tReturnDTO = client.path("surpolicy/sendBean").accept(MediaType.APPLICATION_XML).acceptEncoding("utf-8").post(tUserBean,ReturnDTO.class );  
  29.   
  30.     System.out.println("返回的数据:" + tReturnDTO.getMsg());  
  31.   
  32. }  


六、服务端与客户端完整代码如下:

1、服务端

[java] view plain copy
  1. package cn.rest.rest;  
  2.   
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.POST;  
  5. import javax.ws.rs.Path;  
  6. import javax.ws.rs.PathParam;  
  7. import javax.ws.rs.Produces;  
  8. import javax.ws.rs.core.MediaType;  
  9.   
  10. import cn.rest.bean.UserBean;  
  11. import cn.rest.bean.response.ReturnDTO;  
  12. import cn.rest.util.ObjectAndXmlHandle;  
  13.   
  14. /** 
  15.  *  
  16.  * SurpolicyEntrence.java 
  17.  *  
  18.  * @title CXF RESTful风格WebService 
  19.  * @description 
  20.  * @author SAM-SHO 
  21.  * @Date 2014-11-24 
  22.  */  
  23. @Path(value = "/surpolicy")  
  24. public class SurpolicyEntrence {  
  25.   
  26.     /** 
  27.      * 简单服务方法 
  28.      * @param input 
  29.      * 访问地址:http://localhost:8080/Java_WS_Server/rest/surpolicy/sendString/queryParams_aa 
  30.      * @return 
  31.      */  
  32.     @GET  
  33.     @Path("/sendString/{input}")  
  34.     // @Produces("text/plain")  
  35.     @Produces(MediaType.TEXT_PLAIN)  
  36.     public String sendStringParam(@PathParam("input") String input) {  
  37.         System.out.println("接收的参数: \r\n" + input);  
  38.         String tReturn = "成功返回";  
  39.         return tReturn;  
  40.   
  41.     }  
  42.   
  43.     /** 
  44.      * 接受XML ,推荐使用。 
  45.      * 地址:http://localhost:8080/Java_WS_Server/rest/surpolicy/sendXml  
  46.      * 设置 Content-Type: APPLICATION/XML(可以不设)  
  47.      * body 中设置 xml内容 
  48.      */  
  49.     @POST  
  50.     @Path("/sendXml")  
  51.     @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  52.     public String sendXml(String requestXML) {  
  53.         System.out.println("接收的参数:\r\n " + requestXML);  
  54.           
  55.         UserBean tUserBean = ObjectAndXmlHandle.parseXml2OUserBean(requestXML);  
  56.           
  57.         String tReturn = tUserBean.getName()+ " 你好,你的请求成功返回";  
  58.         return tReturn;  
  59.     }  
  60.   
  61.     /** 
  62.      *接收Bean 
  63.      *  
  64.      * @param user 
  65.      * http://localhost:8080/Java_WS_Server/rest/surpolicy/sendBean 
  66.      * 需要设置 Content-Type: application/xml 
  67.      * body 中设置 xml内容 
  68.      * @return 
  69.      */  
  70.     @POST  
  71.     @Path("/sendBean")  
  72.     @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  73.     public ReturnDTO sendBean(UserBean user) {  
  74.   
  75.         //转成报文  
  76.         ObjectAndXmlHandle.Object2Xml(user);  
  77.         System.out.println(user.getUserAddress().getHomeAddress());  
  78.           
  79.         ReturnDTO tReturnDTO = new ReturnDTO();  
  80.         tReturnDTO.setCode("1");  
  81.         tReturnDTO.setMsg(user.getName()+ " ,请求成功,已返回");  
  82.           
  83.         return tReturnDTO;  
  84.     }  
  85. }  

2、客户端

[java] view plain copy
  1. package cn.rest.client;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.ws.rs.core.MediaType;  
  7.   
  8. import org.apache.cxf.jaxrs.client.ClientConfiguration;  
  9. import org.apache.cxf.jaxrs.client.WebClient;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12.   
  13. import cn.rest.bean.UserAddress;  
  14. import cn.rest.bean.UserBean;  
  15. import cn.rest.bean.UserPhone;  
  16. import cn.rest.bean.response.ReturnDTO;  
  17.   
  18. public class RestClient {  
  19.   
  20.     private static WebClient client;  
  21.   
  22.   
  23.     /** 
  24.      * @param args 
  25.      */  
  26.     public static void main(String[] args) {  
  27.         RestClient tRestClient = new RestClient();  
  28.         tRestClient.init();  
  29.           
  30.         //1-简单测试  
  31. //      tRestClient.sendString();  
  32.           
  33.         // 2-发送XML报文  
  34. //      tRestClient.sendRequestXml();  
  35.   
  36.         // 2-发送Bean  
  37.         tRestClient.sendRequestBean();  
  38.     }  
  39.       
  40.   
  41.   
  42.     public void init() {  
  43.         // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址  
  44.         // String url = "http://localhost:8080/Java_WS_Server/rest/";  
  45.         // client = WebClient.create(url);  
  46.   
  47.         // 从Spring Ioc容器中拿webClient对象,或者直接用注入  
  48.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  49.         client = ctx.getBean("webClient", WebClient.class);  
  50.   
  51.     }  
  52.       
  53.     /** 
  54.      * 测试服务端的简单方法 
  55.      */  
  56.     public void sendString(){  
  57.         String tResponseMsg = client.path("surpolicy/sendString/{input}","我来ping一下。。").accept(MediaType.TEXT_PLAIN).get(String.class);  
  58.         System.out.println(tResponseMsg);  
  59.     }  
  60.       
  61.     /** 
  62.      * 发送XML报文 
  63.      */  
  64.     private void sendRequestXml() {  
  65.         String tRequestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><USER><AGE>27</AGE><NAME>SAM-SHO</NAME><PhoneList><UserPhone><num>13612345678</num><type>移动</type></UserPhone><UserPhone><num>13798765432</num><type>联通</type></UserPhone></PhoneList><UserAddress><homeAddress>苏州高新区</homeAddress><workAddress>苏州园区</workAddress></UserAddress></USER>";  
  66.         String tResponseMsg = client.path("surpolicy/sendXml").accept(MediaType.APPLICATION_XML).post(tRequestXml, String.class);  
  67.         System.out.println("返回的信息: \r\n" + tResponseMsg);  
  68.     }  
  69.       
  70.       
  71.     /** 
  72.      * 发送Bean 
  73.      */  
  74.     private void sendRequestBean() {  
  75.         UserBean tUserBean = new UserBean();  
  76.         tUserBean.setName("SAM-SHO");  
  77.         tUserBean.setAge("27");  
  78.           
  79.         UserAddress tUserAddress = new UserAddress();  
  80.         tUserAddress.setWorkAddress("苏州园区");  
  81.         tUserAddress.setHomeAddress("苏州高新区");  
  82.         tUserBean.setUserAddress(tUserAddress);  
  83.           
  84.         List<UserPhone> phoneList = new ArrayList<UserPhone>();  
  85.         UserPhone tUserPhone =  new UserPhone();  
  86.         tUserPhone.setType("移动");  
  87.         tUserPhone.setNum("13612345678");  
  88.         phoneList.add(tUserPhone);  
  89.           
  90.         tUserPhone =  new UserPhone();  
  91.         tUserPhone.setType("联通");  
  92.         tUserPhone.setNum("13798765432");  
  93.         phoneList.add(tUserPhone);  
  94.         tUserBean.setPhoneList(phoneList);  
  95.           
  96.         ClientConfiguration config = WebClient.getConfig(client);  
  97.         config.getHttpConduit().getClient().setReceiveTimeout(90000);//设置超时  
  98.         ReturnDTO tReturnDTO = client.path("surpolicy/sendBean").accept(MediaType.APPLICATION_XML).acceptEncoding("utf-8").post(tUserBean,ReturnDTO.class );  
  99.   
  100.         System.out.println("返回的数据:" + tReturnDTO.getMsg());  
  101.       
  102.     }  
  103.   
  104.   
  105.   
  106.   
  107. }  

阅读全文
0 0
原创粉丝点击