我是apache camel的新手。我想用apache camel做一些简单的任务。我在这个url http://localhost:8080/users/上有可用的json数据。有来自我的rest服务的json数据。但是我想得到这个json数据,用apache camel将它解组到POJO,并将一些数据写入txt文件。但是我的应用程序启动后就停不下来了。
这是我在http://localhost:8080/users/上提供的JSON数据
[{"id":1,"login":"admin","password":"admin","passwordAgain":null,"email":"admin@admin.com","firstName":"Admin","lastName":"Adminovich","birthday":"2010-10-10","role":{"id":1,"name":"admin"}},
{"id":5,"login":"Snow123","password":"1111","passwordAgain":null,"email":"john@snow.com","firstName":"John","lastName":"Snow","birthday":"2010-10-10","role":{"id":2,"name":"user"}}]
spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="MyRouteBuilder" class="MyRouteBuilder" init-method="restart">
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="MyRouteBuilder"/>
</camelContext>
</beans>
我的路线生成器
import Mapping.User;
import Mapping.Users;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.component.restlet.RestletConstants;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
public class MyRouteBuilder extends RouteBuilder {
private String routePort = null;
private String routeAnswer = null;
public void restart() {
String start = "Route Property Placeholder Example started on port" + " 8080" + "!";
System.out.println(start);
}
public void configure() throws Exception {
from("timer:foo?repeatCount=1").to("http://10.10.34.145:8080/users/").unmarshal().json(JsonLibrary.Jackson, Users.class ).log("STARTED!")
.process(new Processor() {
public void process (Exchange exchange) throws Exception {
Users usersList = exchange.getIn().getBody(Users.class);
exchange.getIn().setBody(usersList.usersList);
System.out.println(usersList.usersList.size());
}
}).to("file:///home/name/username/Desktop/camel.txt").stop();
}
}
用户名
package Mapping;
import com.google.gson.FieldNamingStrategy;
import java.lang.reflect.Field;
public class User {
public User() {
}
private int id;
private String login;
private String password;
private String passwordAgain;
private String email;
private String firstName;
private String lastName;
private String birthday;
public void setId(int id) {
this.id = id;
}
public void setLogin(String login) {
this.login = login;
}
public void setPassword(String password) {
this.password = password;
}
public void setPasswordAgain(String passwordAgain) {
this.passwordAgain = passwordAgain;
}
public void setEmail(String email) {
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void setRole(String role) {
this.role = role;
}
public int getId() {
return id;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
public String getPasswordAgain() {
return passwordAgain;
}
public String getEmail() {
return email;
}
public String getFirsteName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getBirthday() {
return birthday;
}
public String getRole() {
return role;
}
private String role;
}
使用者
package Mapping;
import java.util.ArrayList;
import java.util.List;
public class Users {
public List<User> usersList = new ArrayList<User>();
}
主要
import org.apache.camel.main.Main;
public class MainClass {
public static void main(String[] args) {
Main main = new Main();
main.enableHangupSupport();
MyRouteBuilder rb = new MyRouteBuilder();
main.addRouteBuilder(rb);
try {
main.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2条答案
按热度按时间kcrjzv8t1#
在我的例子中,我将JsonData转换为Java对象,并使用以下依赖项创建了一个自定义CSV文件。
您可以创建简单的文本文件或CSV文件,无论它是什么。这是我的路线:
这是我的处理器,我在其中将JsonString传递给用于Pojo的ObjectMapper:
如果你不知道如何使用ObjectMapper,这个链接可以帮助你。https://www.youtube.com/watch?v=02X6U81MdJ0&t=1s
uxh89sit2#
在json的情况下使用以下金额之一:Jackson或格森
我建议检查documentation
您可以直接在路由上执行解组:
unmarshal().json(JsonLibrary.Jackson, MyModel.class)
或者您可以创建一个处理器:
祝你好运