如何使用apache camel从soap响应中获取值?
Posted
技术标签:
【中文标题】如何使用apache camel从soap响应中获取值?【英文标题】:How to get the value value from soap response using apache camel? 【发布时间】:2019-02-08 03:44:21 【问题描述】:我想从下面的肥皂响应中获取 AddResult 的值,即 100:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>100</AddResult></AddResponse></soap:Body></soap:Envelope>
下面是java代码:
package com.learncamel.routes.xmlxstream;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
import com.learncamel.domain.AddResponse;
public class XmlResponseToObject extends RouteBuilder
@Override
public void configure() throws Exception
// TODO Auto-generated method stub
Map<String, String> reference = new HashMap<String, String>();
reference.put("AddResponse ", AddResponse.class.getName());
XStreamDataFormat xstreamDataFormat = new XStreamDataFormat();
xstreamDataFormat.setAliases(reference);
xstreamDataFormat.setPermissions(AddResponse.class.getName());
try
from("direct:xmlinput")
.log("Received Message before unmarshal is $body and Headers are $headers")
.unmarshal(xstreamDataFormat)
.log("Received Message after unmarshal is $body and Headers are $headers")
.to("log:?level=INFO&showBody=true");
catch(Exception e)
e.printStackTrace();
下面是junit代码:
package com.learncamel.routes.xmlxstream;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class XmlResponseToObjectTest extends CamelTestSupport
@Override
public RoutesBuilder createRouteBuilder() throws Exception
return new XmlResponseToObject();
@Test
public void unMarshalTest() throws InterruptedException
//AddResponse addresponse=new AddResponse();
String input="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><AddResponse xmlns=\"http://tempuri.org/\"><AddResult>100</AddResult></AddResponse></soap:Body></soap:Envelope>";
template.sendBody("direct:xmlinput",input);
下面是 POJO:
package com.learncamel.domain;
public class AddResponse
private String AddResult;
public String getAddResult()
return AddResult;
public void setAddResult(String addResult)
AddResult = addResult;
堆栈跟踪
com.thoughtworks.xstream.mapper.CannotResolveClassException:信封 在 com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71) 在 com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) 在 com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47) 在 com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133) 在 com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) 在 com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1486) 在 com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1454) 在 org.apache.camel.dataformat.xstream.AbstractXStreamWrapper.unmarshal(AbstractXStreamWrapper.java:374) 在 org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69) 在 org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:138) 在 org.apache.camel.processor.Pipeline.process(Pipeline.java:101) 在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在 org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) 在 org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186) 在 org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86) 在 org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:541) 在 org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:506) 在 org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369) 在 org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:506) 在 org.apache.camel.impl.ProducerCache.send(ProducerCache.java:229) 在 org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144) 在 org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161) 在 org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:168) 在 com.learncamel.routes.xmlxstream.XmlResponseToObjectTest.unMarshalTest(XmlResponseToObjectTest.java:25) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在 org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) 在 org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) 在 org.junit.rules.RunRules.evaluate(RunRules.java:20) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
【问题讨论】:
【参考方案1】:您可以尝试使用 xpath 从 xml 中提取值,例如:
.convertBodyTo(org.w3c.dom.Document.class)
.setProperty("needValue",xpath("*//AddResult/text()",String.class))
//use extracted value
如果需要将输入的 xml 转换为另一个寻找 xslt 组件。
更新:如果你真的需要使用对象来删除你的异常,你需要提取映射的主体 AddResponse 因为你映射了它。您的输入字符串从未映射的肥皂元素“信封”开始。如果您在 .unmarshall() 之前有 CxfPayload,只需将其转换为 Document 或 String。要成功测试,只需删除肥皂元素。
【讨论】:
【参考方案2】:public static Document loadXMLString(String response) throws Exception
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(response));
return db.parse(is);
public static List<String> getFullNameFromXml(String response, String tagName) throws Exception
Document xmlDoc = loadXMLString(response);
NodeList nodeList = xmlDoc.getElementsByTagName(tagName);
List<String> ids = new ArrayList<String>(nodeList.getLength());
for(int i=0;i<nodeList.getLength(); i++)
Node x = nodeList.item(i);
ids.add(x.getFirstChild().getNodeValue());
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
return ids;
public static void main(String arg[]) throws Exception
String res="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SubtractResponse xmlns=\"http://tempuri.org/\"><SubtractResult>150</SubtractResult></SubtractResponse></soap:Body></soap:Envelope>";
List<String> output = XmlResponseToObject.getFullNameFromXml(res, "SubtractResult");
String[] strarray = new String[output.size()];
output.toArray(strarray);
String x = output.get(0);
System.out.println(x);
【讨论】:
以上是关于如何使用apache camel从soap响应中获取值?的主要内容,如果未能解决你的问题,请参考以下文章
使用 apache-camel [2.24.3] 使用 SOAP 服务时出错
Apache Camel ProducerTemplate忽略SSL证书检查
如何在apache camel DSL中获得正确的https服务器响应?