在 SOAP 请求中使用 Java、return 读取 XML 文件
Reading XML Files using Java, return in a SOAP request
我对使用 SOAP 和蓝图还很陌生(就像 Spring)。
不管怎样,我只是想学习atm的基础知识,到目前为止做得很好。
我 运行 在使用 Java Class 从 XML 文件中检索特定节点值时遇到了一个小问题。这在我 运行 应用程序作为独立应用程序时有效,但是当我使用 Soap 获取请求时,值 "lastName" returns 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 应用程序时它实际上 return 姓氏,但在 SOAP 请求中我得到 "LastName: null".
啊!!我发现了错误。傻我。所以,我在我的蓝图中调用方法 "returnLastName" 并且它返回 null,我没有意识到这个方法被称为 ALONE,所以将我的代码从 main 移动到方法中修复它就像一个魅力哈哈。
我觉得自己真的很傻,但让我失望的总是小错误。
我对使用 SOAP 和蓝图还很陌生(就像 Spring)。 不管怎样,我只是想学习atm的基础知识,到目前为止做得很好。
我 运行 在使用 Java Class 从 XML 文件中检索特定节点值时遇到了一个小问题。这在我 运行 应用程序作为独立应用程序时有效,但是当我使用 Soap 获取请求时,值 "lastName" returns 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 应用程序时它实际上 return 姓氏,但在 SOAP 请求中我得到 "LastName: null".
啊!!我发现了错误。傻我。所以,我在我的蓝图中调用方法 "returnLastName" 并且它返回 null,我没有意识到这个方法被称为 ALONE,所以将我的代码从 main 移动到方法中修复它就像一个魅力哈哈。
我觉得自己真的很傻,但让我失望的总是小错误。