Apache Camel整合Spring Boot遇JAXB数据格式创建失败问题求助
Apache Camel JAXB依赖配置问题排查请求
我正在学习中间件技术,首先接触了Apache Camel。尝试实现XML转JSON功能时,无法正确配置JAXB依赖,始终报错**"Data format 'jaxb' could not be created"**。已尝试调整依赖版本但问题仍存在,现将相关代码、错误栈及POM文件附上,请求排查问题原因。
相关代码
Application.java
package com.SpringWithCamel.Camel.boot; import org.apache.camel.main.Main; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { Main main = new Main(); main.configure().addRoutesBuilder(new XmlToJsonRoute()); main.run(args); } }
XmlToJsonRoute.java
package com.SpringWithCamel.Camel.boot; import org.apache.camel.builder.RouteBuilder; public class XmlToJsonRoute extends RouteBuilder { @Override public void configure() { from("file:C:/Users/akstu/Downloads/Camel.boot/Camel.boot/input?noop=true") .log("Processing file: ${file:name}") .unmarshal().jaxb("com.SpringWithCamel.Camel.boot.model") .marshal().json() .to("file:C:/Users/akstu/Downloads/Camel.boot/Camel.boot/output?fileName=output.json") .log("Saved JSON file to output directory"); } }
User.java
package com.SpringWithCamel.Camel.boot.model; import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "user") public class User { public int id; public String name; public String email; public User() {} @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
user.xml
<user> <id>101</id> <name>John Doe</name> <email>john@example.com</email> </user>
错误栈信息
at com.SpringWithCamel.Camel.boot.Application.main(Application.java:15) Caused by: java.lang.IllegalArgumentException: Data format 'jaxb' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath at org.apache.camel.reifier.dataformat.DataFormatReifier.createDataFormat(DataFormatReifier.java:289)
Process finished with exit code 1
POM.xml配置
<groupId>com.SpringWithCamel</groupId> <artifactId>Camel.boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Camel.boot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>21</java.version> <camel.version>4.10.2</camel.version> </properties> <dependencies> <!-- Spring Boot Starter Web for general web functionalities --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Camel Spring Boot Starter --> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>${camel.version}</version> </dependency> <!-- JAXB API for XML binding --> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>3.0.1</version> </dependency> <!-- JAXB Runtime for unmarshalling XML --> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>3.0.2</version> </dependency> <!-- Camel Core engine --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel.version}</version> </dependency> <!-- File component for file-based routes --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-file</artifactId> <version>${camel.version}</version> </dependency> <!-- Camel JAXB for XML to Java (Unmarshalling) --> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-jaxb-starter</artifactId> <version>${camel.version}</version> </dependency> <!-- Jackson for Java to JSON (Marshalling) --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jackson</artifactId> <version>${camel.version}</version> </dependency> <!-- Spring Boot Starter Test for unit tests --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
内容的提问来源于stack exchange,提问作者ABid AK
相关产品推荐
相关产品推荐

