【问题标题】:Reading XML Files using Java, return in a SOAP request使用 Java 读取 XML 文件,在 SOAP 请求中返回
【发布时间】:2017-12-07 12:45:33
【问题描述】:

我对使用 SOAP 和蓝图(就像 Spring)相当陌生。 不管怎样,我只是想学习基本的atm,到目前为止做得很好。

我在使用 Java 类从 XML 文件中检索特定节点值时遇到了一个小问题。这在我作为独立运行应用程序时有效,但是当我使用 Soap 获取请求时,值“lastName”返回 null。

public static void main(String[] args) throws XPathExpressionException {

    DocumentBuilderFactory builderFactory =
            DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException p) {
        p.printStackTrace();
    }
    try {
    Document document = builder.parse(new FileInputStream("d:\\input11.xml")); 
    XPath xP = XPathFactory.newInstance().newXPath();
    String expression ="/people/person/lastName";
    NodeList nodeList = (NodeList) xP.compile(expression).evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        lastName += nodeList.item(i).getFirstChild().getNodeValue() + " ";
    }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException s) {
        s.printStackTrace();
    }

    System.out.println(lastName);
}

public static String returnLastName(String input){
System.out.println(lastName);

return "LastName: "+lastName +"\n";

} 

}

这是我的 blueprint.xml 代码:

    <bean id="lastNameBean" class="com.*****.camelBlueprintTest.XMLCamel" />
    <route id="lastName">
<from uri="cxf:bean:returnLName" />
<bean ref="lastNameBean" method="returnLastName" />
    <log message="The message contains ${body}" />
    <to uri="mock:result" />
</route>

所以当我运行 Java 应用程序时它确实返回了姓氏,但在 SOAP 请求中我得到“LastName: null”。

【问题讨论】:

    标签: spring web-services soap cxf blueprint


    【解决方案1】:

    啊!!我发现了错误。傻我。所以,我在我的蓝图“returnLastName”中调用了该方法,它返回 null,我没有意识到这个方法被单独调用,所以将我的代码从 main 移动到方法中修复它就像一个魅力哈哈。

    我觉得自己很傻,但总是让我犯错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多