使用带有javax.xml.soap的NWS soap服务器

时间:2014-07-06 21:30:15

标签: java xml soap

我正在尝试使用NWS soap服务器来获得纬度/经度。我尝试了几种方法,而我所使用的方法使用了javax.xml.soap。当我尝试时,jax-ws方法立即失败:wsimport -keep http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl。我收到一个错误,表明它无法处理编码。这是我现在正在处理的来源:

package soapexample;
import javax.xml.soap.*;

public class SoapExample {

public static void main(String args[]) throws Exception {
    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    // Send SOAP Message to SOAP Server
    String url = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
    SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

    // print SOAP Response
    System.out.print("Response SOAP Message:");
    soapResponse.writeTo(System.out);

    soapConnection.close();
}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "http://www.weather.gov/";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("xsi", serverURI);

    /*

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("NDFDgenByDay", "xsi");
    soapBody.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("startDate", "xsi");
    soapBodyElem1.addTextNode("2014-07-06");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("numDays", "xsi");
    soapBodyElem2.addTextNode("3");
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Unit", "xsi");
    soapBodyElem3.addTextNode("e");
    SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("format", "xsi");
    soapBodyElem4.addTextNode("24 hourly");
    SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("latitude", "xsi");
    soapBodyElem5.addTextNode("38.99");
    SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("longitude", "xsi");
    soapBodyElem6.addTextNode("-77.01");
    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println();

    return soapMessage;
}
`

它几乎有效,我得到了这个回复:

<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type="xsd:string">SERVER</faultcode>
<faultactor xsi:type="xsd:string"></faultactor>
<faultstring xsi:type="xsd:string">format needs to be either 24 hourly or 12             hourly</faultstring>
<detail xsi:type="xsd:string">input format was &quot;-77.01&quot;</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</code></pre>

此时我迷失了。我的请求无法解决问题。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

在对我的问题很感兴趣之后,我决定再次谷歌自杀,并找到了一篇关于为什么国家气象局不能与JAX-WS合作的优秀文章。他们甚至提供了使用SAAJ的工作示例。链接是:

calling rpc encoded web services

答案 1 :(得分:0)

不确定这对您是否真的有帮助,但我前段时间处理了NWS点预测并使我的控​​件可嵌入:http://www.iontech.org/Weather

干杯