我是阿帕奇骆驼新手。我想用apache camel做一些简单的任务。我在此网址http://localhost:8080/users/上提供了json数据。我的休息服务有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>
MyRouteBuilder
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();
}
}
}
答案 0 :(得分:0)
在我的例子中,我将JsonData转换为Java对象,并使用以下依赖项创建了一个自定义CSV文件。
您可以创建简单的文本文件或CSV,无论它是什么。 这是我的路线:
public async Task<IActionResult> PostComment([FromBody] Comment comment)
{
comment.AuthorID = Utilities.GetUserId(this.User);
comment.CreatedAt = DateTime.Now;
if (!ModelState.IsValid) {
....
这是我的处理器,我将JsonString传递给Pojo的ObjectMapper:
from("couchdb:http://localhost:5984/order")
.startupOrder(1)
.process(new JsonToCsvProcessor())
.to("file:/home/r2/Desktop/ofBizFile?fileExist=append&fileName=orders-${date:now:dd-MM-yyyy}.csv");
如果您不知道如何使用ObjectMapper,此链接将帮助您。 https://www.youtube.com/watch?v=02X6U81MdJ0&t=1s
答案 1 :(得分:0)
使用json的情况之一:Jackson或Gson
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-gson</artifactId>
<version>${camel.version}</version>
</dependency>
我建议检查documentation
您可以在路线上直接进行海警:
unmarshal().json(JsonLibrary.Jackson, MyModel.class)
或者您可以创建处理器:
@Component
public class MyModelUnMarshalProccessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
MyModel model = exchange.getIn().getBody(MyModel.class);
exchange.getOut().setBody(model);
exchange.getOut().setHeader("id", body.getId());
}
}
@Autowired
MyModelUnMarshalProccessor myModelUnMarshalProccessor;
from("{{fromServices}}")
.log("${body}")
.process(myModelMarshalProccessor)
祝你好运