gpt4 book ai didi

java - Apache Camel 中的解码 (JSON)

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:23 25 4
gpt4 key购买 nike

我是 Apache Camel 的新手。我想使用 apache Camel 做一些简单的任务。我在此网址 http://localhost:8080/users/ 上有可用的 json 数据。有来 self 的其余服务的 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();
}
}

}

最佳答案

就我而言,我将 JsonData 转换为 Java 对象,并使用以下依赖项创建了自定义 CSV 文件。

  • jackson 映射器-asl
  • jackson-core-asl

您可以创建简单的文本文件或 CSV 文件,无论它是什么。这是我的路线:

    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");

这是我的处理器,我将 JsonString 传递给 Pojo 的 ObjectMapper:

{

public void process(Exchange exchange)throws Exception{
String jsonString = exchange.getIn().getBody(String.class);
exchange.getOut().setBody(convertJsonToCSV(jsonString).toString());
}

public StringBuilder convertJsonToCSV(String jsonStringArgs){

StringBuilder csvFile = new StringBuilder();
//Here customize data format using append()
.
.
return csvFile;
}

}

如果您不知道如何使用 ObjectMapper,此链接将为您提供帮助。 https://www.youtube.com/watch?v=02X6U81MdJ0&t=1s

关于java - Apache Camel 中的解码 (JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37481693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com