我在这里从互联网上获取了一系列货币:http://dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml
现在我想把它变成一个Java对象,所以我可以用它来做。我正在尝试使用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;
}
}
我创建了一个名为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;
}
}
当我运行代码时,我收到此错误:
java.lang.IllegalArgumentException:无法找到名称为的数据格式: JAXB
我的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>
答案 0 :(得分:0)
首先对不起我的简短回答,但我正在通过我的手机写作。
但是这里有我的项目,我做这样的事情,请看,我希望它可以帮到你。
你可以在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
}
你只需要在邮件标题'Content-Type = application / xml'中发送send,它就可以了。 但是,如果你想用XML转换你的POJO,你需要做这样的事情:
@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();
}