简体   繁体   English

如何在Apache骆驼中将XML转换为Java对象?

[英]How to convert XML to Java object in Apache camel?

I am getting a list of currencies from the Internet here: http://dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml 我正在从Internet上获得货币列表: http : //dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml

Now I want to make it into a Java object so I can do stuff with it. 现在,我想将其放入Java对象中,以便对其进行处理。 I am trying to use jaxbUnmarshal but I am open to whatever. 我正在尝试使用jaxbUnmarshal,但是我愿意接受任何东西。

package com.domain.subdomain.routes;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JaxbDataFormat;

public class CurrencyRoute extends RouteBuilder {


    private String currencyWsURL; // http://dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml from Spring template

    @Override
    public void configure() {
        from("quartz://myTimer?trigger.repeatCount=0")
                .log("### Quartz trigger ###")
                .to("direct:readFile");

        from("direct:readFile")
                .log("### Read file ###")
                .to("https4://" + currencyWsURL)
                .to("dataformat:jaxb:unmarshal")
                .log("${body}");
    }

    public void setCurrencyWsURL(String currencyWsURL) {
        this.currencyWsURL = currencyWsURL;
    }
}

I have created a Java object named Currency: 我创建了一个名为Currency的Java对象:

package com.domain.subdomain.domain;

public class Currency {
    String country;
    String isoCode;
    String code;
    Double unit;
    String name;

    Double transferPurcase;
    Double transferSell;
    Double transferChanges;
    Double transferPrevious;
    Double transferMiddleCourse;

    Double notePurcase;
    Double noteSell;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Double getUnit() {
        return unit;
    }

    public void setUnit(Double unit) {
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getTransferPurcase() {
        return transferPurcase;
    }

    public void setTransferPurcase(Double transferPurcase) {
        this.transferPurcase = transferPurcase;
    }

    public Double getTransferSell() {
        return transferSell;
    }

    public void setTransferSell(Double transferSell) {
        this.transferSell = transferSell;
    }

    public Double getTransferChanges() {
        return transferChanges;
    }

    public void setTransferChanges(Double transferChanges) {
        this.transferChanges = transferChanges;
    }

    public Double getTransferPrevious() {
        return transferPrevious;
    }

    public void setTransferPrevious(Double transferPrevious) {
        this.transferPrevious = transferPrevious;
    }

    public Double getTransferMiddleCourse() {
        return transferMiddleCourse;
    }

    public void setTransferMiddleCourse(Double transferMiddleCourse) {
        this.transferMiddleCourse = transferMiddleCourse;
    }

    public Double getNotePurcase() {
        return notePurcase;
    }

    public void setNotePurcase(Double notePurcase) {
        this.notePurcase = notePurcase;
    }

    public Double getNoteSell() {
        return noteSell;
    }

    public void setNoteSell(Double noteSell) {
        this.noteSell = noteSell;
    }
}

When I run the code i get this error: 当我运行代码时,出现此错误:

java.lang.IllegalArgumentException: Cannot find data format with name: jaxb java.lang.IllegalArgumentException:找不到名称为jaxb的数据格式

My pom.xml: 我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.domain.subdomain</groupId>
    <artifactId>xml-to-object</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>spi-annotations</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz</artifactId>
            <version>2.20.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jaxb -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jaxb</artifactId>
            <version>2.20.2</version>
            <scope>test</scope>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- Allows the routes to be run via 'mvn camel:run' -->
            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>2.20.0</version>
            </plugin>
        </plugins>
    </build>
</project>

First of all sorry for my short answer, but I'm writinning from my mobile. 首先,抱歉,我的回答很短,但是我正在用手机写信。

But here has my project where I do something like this, please, look and I hope it help you. 但是,这里有我的项目,我在其中做类似的事情,请看,希望对您有所帮助。

https://github.com/vtripeno/integration_camel_apis/blob/master/Camel/src/main/java/br/com/camel/integration/credit/user/routes/IntegrationRoute.java https://github.com/vtripeno/integration_camel_apis/blob/master/Camel/src/main/java/br/com/camel/integration/credit/user/routes/IntegrationRoute.java

You can do somthing like this in your POJO: 您可以在POJO中执行以下操作:

@ApiModel(description = "The Credit contract.")
@Validated
@XmlRootElement(name = "root_element")
@XmlAccessorType(XmlAccessType.FIELD)
public class POJO implements Serializable {

@JsonProperty("data")
@ApiModelProperty(value = "The credit proposal object")
private Stirng data;
// GETTER AND SETTER
}

And you just can send send in the message header 'Content-Type = application/xml', it works OK. 您只需要在消息头“ Content-Type = application / xml”中发送send,就可以了。 But, if you want to convert your POJO in XML you need to do something like this: 但是,如果要将POJO转换为XML,则需要执行以下操作:

@Component
public class IntegrationRoute extends RouteBuilder {
@Override
public void configure() throws Exception {

    XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setForceTopLevelObject(true);
    xmlJsonFormat.setEncoding("UTF-8");
    xmlJsonFormat.setForceTopLevelObject(true);
    xmlJsonFormat.setTrimSpaces(true);
    xmlJsonFormat.setRootName("data");
    xmlJsonFormat.setSkipNamespaces(true);
    xmlJsonFormat.setRemoveNamespacePrefixes(true);
    xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));
from("direct:your_route").id("yourRoute")
        .unmarshal(xmlJsonFormat)
        .to("log:end")
        .end();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM