JaxB Marshaling在骆驼

时间:2017-07-03 11:31:22

标签: jaxb apache-camel marshalling

我是Java中的Apache camel和Jax b概念的新手。 我有一个camel队列中的java对象列表。我想用Javs DSL将它转换为xml(不使用spring)。 任何人都可以指导我这样做吗?

我有以下POJO类

public class MyPojo {
private int groupId;
private int memberId;
private String details;
public int getgroupId() {
    return groupId;
}
public void setgroupId(int groupId) {
    this.groupId = groupId;
}
public int getMemberId() {
    return memberId;
}
public void setMemberId(int memberId) {
    this.memberId = memberId;
}
public String getdetails() {
    return details;
}
public void setdetails(String details) {
    this.details = details;
}}

以下是我的jaxb实现的骆驼代码

JaxbDataFormat jaxbMarshal = new JaxbDataFormat();
    jaxbMarshal.setContextPath("com.test");
    jaxbMarshal.setPartClass("com.test.MyPojo");

from("direct:javaObjects") //this direct having the list of MYPojo Objects
    .marshal(jaxbMarshal)
     .to("src/output");

我遇到异常(我在类路径中为jaxb添加了maven依赖)

Failed to create route route4 at: >>> Marshal[org.apache.camel.model.dataformat.JaxbDataFormat@3feb2dda] <<< in route: Route(route4)[[From[direct:javaObjects]] -> [Marshal[org.apa... because of Data format 'jaxb' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath

2 个答案:

答案 0 :(得分:0)

我创建了jaxb.in​​dex文件(来自eclipse的new-&gt;文件)。文件的内容应该是注释类名

在我们的情况下应该是         的 MyPojo

并且需要将其放在上下文路径中。在我们的情况下,它应该被放入          com.test 位置

和带注释的Pojo类是

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
 public class MyPojo {
@XmlElement
private int groupId;
@XmlElement
private int memberId;
@XmlElement
private String details;
public int getgroupId() {
    return groupId;
}
public void setgroupId(int groupId) {
    this.groupId = groupId;
}
public int getMemberId() {
    return memberId;
}
public void setMemberId(int memberId) {
    this.memberId = memberId;
}
public String getdetails() {
    return details;
}
public void setdetails(String details) {
    this.details = details;
}}   

答案 1 :(得分:0)

您可以使用带有注释的pojo:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="MY-POJO")
@XmlType(propOrder = {"groupId", "memberId", "details"})
public class MyPojo {
@XmlElement(name = "groupId")
private int groupId;
@XmlElement(name = "memberId")
private int memberId;
@XmlElement(name = "details")
private String details;
public int getgroupId() {
    return groupId;
}
public void setgroupId(int groupId) {
    this.groupId = groupId;
}
public int getMemberId() {
    return memberId;
}
public void setMemberId(int memberId) {
    this.memberId = memberId;
}
public String getdetails() {
    return details;
}
public void setdetails(String details) {
    this.details = details;
}}