我正在为我的项目使用 Apache Camel
和 Spring Boot
。我有以下代码,在 rtos
端点上通过 http
接收 json 对象
;然后我将它映射到一个 POJO
中,修改它并再次将它发送到另一个端点。
restConfiguration().component("servlet").host("localhost").port("8080").bindingMode(RestBindingMode.auto);
rest("/request").post("/rtos").type(User.class).to("direct:rtos")
from("direct:rtos").process(new Processor() {
public void process(Exchange exchange) throws Exception {
User body = exchange.getIn().getBody(User.class);
System.out.println("Input object: " + body.getName() + ", " + body.getAge());
body.setAge("35");
System.out.println("Output object: " + body.getName() + ", " + body.getAge());
}
}).marshal().json(JsonLibrary.Jackson)
.to("http4://localhost:8080/receive?bridgeEndpoint=true");
from("servlet:/receive").unmarshal().json(JsonLibrary.Jackson, User.class).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
User body = exchange.getIn().getBody(User.class);
body.setAge("100");
System.out.println("Received object: " + body.getName() + ", " + body.getAge());
}
});
这工作正常,这意味着对象被正确操作并通过端点传递。发生的情况是,在发送 http 请求后,我收到 500 Internal Server Error
和以下错误:
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
我注意到如果我设法将某些东西返回到第一条路线,错误就会消失,例如
[...]
.to("http4://localhost:8080/receive?bridgeEndpoint=true").transform().constant("End of route");
我想知道这是为什么,它与 jackson 序列化有什么关系。如果我不想对第一个请求发送回复怎么办?我红this和 this ,我尝试修改我的代码如下:
from("direct:rtos").setExchangePattern(ExchangePattern.InOnly).process(new Processor() {
public void process(Exchange exchange) throws Exception {
User body = exchange.getIn().getBody(User.class);
System.out.println("Input object: " + body.getName() + ", " + body.getAge());
body.setAge("35");
System.out.println("Output object: " + body.getName() + ", " + body.getAge());
}
}).marshal().json(JsonLibrary.Jackson)
.inOnly("http4://localhost:8080/receive?bridgeEndpoint=true");
但我得到了同样的错误。
谁能建议我哪里错了?我试图正确理解 Camel 流程的工作原理,所以我可能在某处犯了一些错误。
谢谢,
萨拉
编辑这是我的用户类:
package it.cam.resources;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import java.io.Serializable;
public class User implements Serializable{
@JsonProperty("id")
private String id;
@JsonProperty("name")
private String name;
@JsonProperty("age")
private String age;
@JsonSerialize(using=StringSerializer.class)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonSerialize(using=StringSerializer.class)
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
最佳答案
不确定是否已回答。我建议你使用 Stream Caching收到回复后。这意味着,在您的 route 使用 from("servlet:/receive").streamCaching()
。
关于java - Apache Camel - 向路由发送消息后的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607207/