将jaxb xml消息转换为Apache骆驼(Java)中的基本类型
作者:互联网
我正在尝试为我所有的骆驼(v 2.13.1)路由构建一个全局错误处理程序.如果该错误处理程序出现异常,它将对其进行记录并向团队发送电子邮件.
但是,我在使用多态性和jaxb注释消息时遇到了问题.我所有的消息看起来都与此类似:
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
@XmlType(propOrder = {})
static abstract class Request {
@XmlElement(required = true)
abstract String getThing();
abstract void setThing(final String thing);
}
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
@XmlType(propOrder = {})
static class MyRequest extends Request {
private String name;
@XmlElement(required = true)
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
private String thing;
@Override
@XmlElement(required = true)
public String getThing() {
return thing;
}
public void setThing(final String thing) {
this.thing = thing;
}
}
我正在玩的错误路线如下所示:
from (errorQueue)
.convertBodyTo(Request.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
final Request req = exchange.getIn().getBody(Request.class);
log.info("name = {}, thing = {}", null, req.getThing());
}
});
根据失败的组件,消息可能是MyRequest,MyOtherRequest等.所有消息都继承自Request.这是我正在测试的特定消息:
<myRequest>
<name>some_name</name>
<thing>some_thing</thing>
</myRequest>
当我运行此程序时,出现类似以下错误:
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"myRequest"). Expected elements are <{}request>
如何将jaxb带注释的消息从各种类型转换为某种基本类型,以便可以从基本类中获取信息?
我更喜欢使用接口而不是基类,但是我得到了类似的结果.
解决方法:
可能未为MyRequest类处理元数据,因为没有子类.请尝试在超类上添加@XmlSeeAlso批注:
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
@XmlType(propOrder = {})
@XmlSeeAlso({MyRequest.class})
static abstract class Request {
标签:jaxb,apache-camel,java 来源: https://codeday.me/bug/20191121/2053011.html