我在项目中使用Apache Camel
和Spring Boot
。我有以下代码,我在json object
端点上通过http
收到rtos
;然后我将其映射到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");
我想知道为什么会这样,以及它与杰克逊序列化有什么关系。如果我不想发送第一个请求的回复怎么办?我红了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");
但是我得到了同样的错误
任何人都可以建议我在哪里错了吗?我试图正确理解骆驼流是如何工作的,所以我可能在某处犯了一些错误。
谢谢,
萨拉
编辑这是我的用户类:
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;
}
}