打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过JAXB请求和解析WebService
  1. WeatherWebService的getWeatherbyCityName SOAP1.2为例
  2. 需要两个对象:
  • 请求对象(GetWeatherbyCityName)
  • 响应对象(GetWeatherbyCityNameResponse)

 

Java代码  
  1. package jaxb.soap;  
  2.   
  3. import javax.xml.bind.annotation.XmlElement;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5.   
  6. @XmlRootElement  
  7. public class GetWeatherbyCityName {  
  8.     @XmlElement  
  9.     private String theCityName;  
  10.   
  11.     private GetWeatherbyCityName() {  
  12.     }  
  13.     public static GetWeatherbyCityName create() {  
  14.         return new GetWeatherbyCityName();  
  15.     }  
  16.     public GetWeatherbyCityName theCityName(String theCityName) {  
  17.         this.theCityName = theCityName;  
  18.         return this;  
  19.     }  
  20. }  

 

 

Java代码  
  1. package jaxb.soap;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.xml.bind.annotation.XmlElement;  
  7. import javax.xml.bind.annotation.XmlElementWrapper;  
  8. import javax.xml.bind.annotation.XmlRootElement;  
  9.   
  10. @XmlRootElement  
  11. public class GetWeatherbyCityNameResponse {  
  12.   
  13.     @XmlElementWrapper(name = "getWeatherbyCityNameResult")  
  14.     @XmlElement(name = "string")  
  15.     private List<String> strings = new ArrayList<String>();  
  16.   
  17.     public List<String> getStrings() {  
  18.         return strings;  
  19.     }  
  20.   
  21. }  

 

package-info.java 不能少,给请求对象GetWeatherbyCityName加上命名空间的,修改前缀

Java代码  
  1. @javax.xml.bind.annotation.XmlSchema(  
  2.     namespace = "http://WebXml.com.cn/",  
  3.     elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED  
  4. )  
  5. package jaxb.soap;  

 

 

请求WebService并解析为GetWeatherbyCityNameResponse对象

Java代码  
  1. package jaxb;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URI;  
  7. import java.net.URL;  
  8.   
  9. import javax.xml.bind.JAXBContext;  
  10. import javax.xml.bind.JAXBElement;  
  11. import javax.xml.bind.Marshaller;  
  12. import javax.xml.bind.Unmarshaller;  
  13. import javax.xml.parsers.DocumentBuilderFactory;  
  14. import javax.xml.soap.MessageFactory;  
  15. import javax.xml.soap.SOAPBody;  
  16. import javax.xml.soap.SOAPConstants;  
  17. import javax.xml.soap.SOAPEnvelope;  
  18. import javax.xml.soap.SOAPMessage;  
  19.   
  20. import jaxb.soap.GetWeatherbyCityNameResponse;  
  21. import jaxb.soap.GetWeatherbyCityName;  
  22.   
  23. import org.w3c.dom.Document;  
  24.   
  25. public class JaxbTest {  
  26.   
  27.     private static String uri = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         URL url = URI.create(uri).toURL();  
  31.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  32.   
  33.         connection.setDoOutput(true);  
  34.         connection.setRequestMethod("POST");  
  35.         connection.setRequestProperty("Content-Type""application/soap+xml; charset=utf-8");  
  36.   
  37.         // 发送数据  
  38.         OutputStream outputStream = connection.getOutputStream();  
  39.   
  40.         Document requestDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();  
  41.         Marshaller marshaller = JAXBContext.newInstance(GetWeatherbyCityName.class).createMarshaller();  
  42.         marshaller.marshal(GetWeatherbyCityName.create().theCityName("南京"), requestDocument);  
  43.         SOAPMessage requestSOAPMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();  
  44.         SOAPBody soapBody = requestSOAPMessage.getSOAPBody();  
  45.         soapBody.addDocument(requestDocument);  
  46.         SOAPEnvelope soapEnvelope = requestSOAPMessage.getSOAPPart().getEnvelope();  
  47.         soapEnvelope.removeNamespaceDeclaration("env");  
  48.         soapEnvelope.addNamespaceDeclaration("soap12""http://www.w3.org/2003/05/soap-envelope");  
  49.         soapEnvelope.addNamespaceDeclaration("xsi""http://www.w3.org/2001/XMLSchema-instance");  
  50.         soapEnvelope.addNamespaceDeclaration("xsd""http://www.w3.org/2001/XMLSchema");  
  51.         soapEnvelope.setPrefix("soap12");  
  52.         soapEnvelope.removeChild(soapEnvelope.getHeader());  
  53.         soapBody.setPrefix("soap12");  
  54.         requestSOAPMessage.writeTo(outputStream);  
  55.   
  56.         // 接收数据  
  57.         InputStream inputStream = connection.getInputStream();  
  58.   
  59.         SOAPMessage responseSOAPMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, inputStream);  
  60. //      responseSOAPMessage.writeTo(System.out);  
  61.         Unmarshaller unmarshaller = JAXBContext.newInstance(GetWeatherbyCityNameResponse.class).createUnmarshaller();  
  62.         JAXBElement<GetWeatherbyCityNameResponse> jaxbElement = unmarshaller.unmarshal(responseSOAPMessage.getSOAPBody().extractContentAsDocument(), GetWeatherbyCityNameResponse.class);  
  63.         GetWeatherbyCityNameResponse response = jaxbElement.getValue();  
  64.         System.out.println(response.getStrings());  
  65.   
  66.         outputStream.close();  
  67.         inputStream.close();  
  68.         connection.disconnect();  
  69.     }  
  70.   
  71. }  

 输出结果:

Java代码  
  1. [江苏, 南京, 5823858238.jpg, 2014-7-27 14:55:2225℃/31℃, 726日 多云转雷阵雨, 西风3-4级转东北风3-4级, 1.gif, 4.gif, 今日天气实况:气温:23℃;风向/风力:东北风 2级;湿度:98%;空气质量:暂无;紫外线强度:中等, 穿衣指数:热,适合穿T恤、短薄外套等夏季服装。  
  2. 过敏指数:暂无。  
  3. 运动指数:较适宜,请适当降低运动强度并注意户外防风。  
  4. 洗车指数:较不宜,风力较大,洗车后会蒙上灰尘。  
  5. 晾晒指数:适宜,天气不错,抓紧时机让衣物晒太阳吧。  
  6. 旅游指数:暂无。  
  7. 路况指数:干燥,天气较好,路面较干燥,路况较好。  
  8. 舒适度指数:较不舒适,多云,有些热。  
  9. 空气污染指数:暂无。  
  10. 紫外线指数:中等,涂擦SPF大于15、PA+防晒护肤品。, 25℃/30℃, 727日 雷阵雨, 东南风3-4级, 4.gif, 4.gif, 25℃/31℃, 728日 雷阵雨转阴, 东南风3-4级, 4.gif, 2.gif, 南京简称“宁”,别名“金陵”,也曾称建业、建康、石头城等,现为江苏省省会,是长江下游西部的中心城市。它位于江苏省西部,东依宁镇山脉,地势险固,风景秀丽。诸葛亮曾对南京一带的山川形势评价说:“钟阜龙蟠,石城虎踞”。南京属北亚热带季风气候区,四季分明,年度最佳气节为秋季(9-11月)。南京是历经苍桑的十代都会。三国鼎立,她目睹群雄角逐争战;六代兴替,她阅尽王朝的曲终幕落;明初,她以举世无双的巍巍城垣显示了泱泱大国之风;晚清,她为近代中国第一个不平等条约被冠上自己的名字而蒙受辱;太平天国,历史在这里风雷激荡;辛亥革命,潮流在这里奔突迂回;抗日战争,日军在这里留下人类历史上最野蛮、最血腥的一页。景观:南京秦淮河,中山陵,玄武湖,莫愁湖,雨花台景区,明孝陵,栖霞山,南京长江大桥。南京是中国的历史文化名城之一,文化古迹比较集中,有新石器时代古文化遗址多处,有三国东吴所筑石头城遗址、南京帝王的陵墓、明代朱元璋的陵墓(明孝陵等)。名胜游览地也很多,主要有中山陵、玄武湖、灵谷寺、秦淮河和栖霞山等。革命纪念地有梅园新村、雨花台等。钟山风景区为国家第一批国家重点风景名胜区,位于南京东北郊,以钟山和玄武湖为中心,是来南京旅游的旅游者的必游之地。这一带主要景观包括中山陵、明孝陵、孙权墓和灵谷寺等。南京山、水、城、林相映成趣,景色壮丽秀美,是中国著名的风景旅游城市。]  

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Jaxb 完全手册
应用 JAXB 把 XML 转换成相应的 JavaBean
jaxb解析xml为对象例子
How to Customize JAXB Bindings for Dates
JAXB--简单应用(一)
Spring-ws示例WebService开发
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服