- 以WeatherWebService的getWeatherbyCityName SOAP1.2为例
- 需要两个对象:
- 请求对象(GetWeatherbyCityName)
- 响应对象(GetWeatherbyCityNameResponse)
package jaxb.soap; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class GetWeatherbyCityName { @XmlElement private String theCityName; private GetWeatherbyCityName() { } public static GetWeatherbyCityName create() { return new GetWeatherbyCityName(); } public GetWeatherbyCityName theCityName(String theCityName) { this.theCityName = theCityName; return this; } }
package jaxb.soap; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class GetWeatherbyCityNameResponse { @XmlElementWrapper(name = "getWeatherbyCityNameResult") @XmlElement(name = "string") private List<String> strings = new ArrayList<String>(); public List<String> getStrings() { return strings; } }
package-info.java 不能少,给请求对象GetWeatherbyCityName加上命名空间的,修改前缀
@javax.xml.bind.annotation.XmlSchema( namespace = "http://WebXml.com.cn/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED ) package jaxb.soap;
请求WebService并解析为GetWeatherbyCityNameResponse对象
package jaxb; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConstants; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import jaxb.soap.GetWeatherbyCityNameResponse; import jaxb.soap.GetWeatherbyCityName; import org.w3c.dom.Document; public class JaxbTest { private static String uri = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; public static void main(String[] args) throws Exception { URL url = URI.create(uri).toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); // 发送数据 OutputStream outputStream = connection.getOutputStream(); Document requestDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Marshaller marshaller = JAXBContext.newInstance(GetWeatherbyCityName.class).createMarshaller(); marshaller.marshal(GetWeatherbyCityName.create().theCityName("南京"), requestDocument); SOAPMessage requestSOAPMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); SOAPBody soapBody = requestSOAPMessage.getSOAPBody(); soapBody.addDocument(requestDocument); SOAPEnvelope soapEnvelope = requestSOAPMessage.getSOAPPart().getEnvelope(); soapEnvelope.removeNamespaceDeclaration("env"); soapEnvelope.addNamespaceDeclaration("soap12", "http://www.w3.org/2003/05/soap-envelope"); soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); soapEnvelope.setPrefix("soap12"); soapEnvelope.removeChild(soapEnvelope.getHeader()); soapBody.setPrefix("soap12"); requestSOAPMessage.writeTo(outputStream); // 接收数据 InputStream inputStream = connection.getInputStream(); SOAPMessage responseSOAPMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, inputStream); // responseSOAPMessage.writeTo(System.out); Unmarshaller unmarshaller = JAXBContext.newInstance(GetWeatherbyCityNameResponse.class).createUnmarshaller(); JAXBElement<GetWeatherbyCityNameResponse> jaxbElement = unmarshaller.unmarshal(responseSOAPMessage.getSOAPBody().extractContentAsDocument(), GetWeatherbyCityNameResponse.class); GetWeatherbyCityNameResponse response = jaxbElement.getValue(); System.out.println(response.getStrings()); outputStream.close(); inputStream.close(); connection.disconnect(); } }
输出结果:
[江苏, 南京, 58238, 58238.jpg, 2014-7-27 14:55:22, 25℃/31℃, 7月26日 多云转雷阵雨, 西风3-4级转东北风3-4级, 1.gif, 4.gif, 今日天气实况:气温:23℃;风向/风力:东北风 2级;湿度:98%;空气质量:暂无;紫外线强度:中等, 穿衣指数:热,适合穿T恤、短薄外套等夏季服装。 过敏指数:暂无。 运动指数:较适宜,请适当降低运动强度并注意户外防风。 洗车指数:较不宜,风力较大,洗车后会蒙上灰尘。 晾晒指数:适宜,天气不错,抓紧时机让衣物晒太阳吧。 旅游指数:暂无。 路况指数:干燥,天气较好,路面较干燥,路况较好。 舒适度指数:较不舒适,多云,有些热。 空气污染指数:暂无。 紫外线指数:中等,涂擦SPF大于15、PA+防晒护肤品。, 25℃/30℃, 7月27日 雷阵雨, 东南风3-4级, 4.gif, 4.gif, 25℃/31℃, 7月28日 雷阵雨转阴, 东南风3-4级, 4.gif, 2.gif, 南京简称“宁”,别名“金陵”,也曾称建业、建康、石头城等,现为江苏省省会,是长江下游西部的中心城市。它位于江苏省西部,东依宁镇山脉,地势险固,风景秀丽。诸葛亮曾对南京一带的山川形势评价说:“钟阜龙蟠,石城虎踞”。南京属北亚热带季风气候区,四季分明,年度最佳气节为秋季(9-11月)。南京是历经苍桑的十代都会。三国鼎立,她目睹群雄角逐争战;六代兴替,她阅尽王朝的曲终幕落;明初,她以举世无双的巍巍城垣显示了泱泱大国之风;晚清,她为近代中国第一个不平等条约被冠上自己的名字而蒙受辱;太平天国,历史在这里风雷激荡;辛亥革命,潮流在这里奔突迂回;抗日战争,日军在这里留下人类历史上最野蛮、最血腥的一页。景观:南京秦淮河,中山陵,玄武湖,莫愁湖,雨花台景区,明孝陵,栖霞山,南京长江大桥。南京是中国的历史文化名城之一,文化古迹比较集中,有新石器时代古文化遗址多处,有三国东吴所筑石头城遗址、南京帝王的陵墓、明代朱元璋的陵墓(明孝陵等)。名胜游览地也很多,主要有中山陵、玄武湖、灵谷寺、秦淮河和栖霞山等。革命纪念地有梅园新村、雨花台等。钟山风景区为国家第一批国家重点风景名胜区,位于南京东北郊,以钟山和玄武湖为中心,是来南京旅游的旅游者的必游之地。这一带主要景观包括中山陵、明孝陵、孙权墓和灵谷寺等。南京山、水、城、林相映成趣,景色壮丽秀美,是中国著名的风景旅游城市。]