Apache Camel - 将消息发送到路由后的响应

ess*_*ara 5 java json apache-camel spring-boot


我正在为我的项目使用Apache Camel和。Spring Boot我有以下代码,我在端点上收到一个json objectvia ;然后我将其映射到 a ,修改它并再次将其发送到另一个端点。httprtosPOJO

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());
    }
});
Run Code Online (Sandbox Code Playgroud)

这工作正常,这意味着对象被正确操作并通过端点。发送 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)
Run Code Online (Sandbox Code Playgroud)

例如,我注意到如果我设法将某些内容返回到第一条路线,错误就会消失

[...]
.to("http4://localhost:8080/receive?bridgeEndpoint=true").transform().constant("End of route");
Run Code Online (Sandbox Code Playgroud)

我想知道这是为什么,以及它与杰克逊序列化有什么关系。如果我不想回复第一个请求怎么办?我红色了thisthis,并尝试按如下方式修改我的代码:

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");
Run Code Online (Sandbox Code Playgroud)

但我得到同样的错误。
谁能建议我哪里错了?我试图正确理解骆驼流程是如何工作的,所以我可能在某个地方犯了一些错误。
谢谢,
萨拉

编辑这是我的用户类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 4

不确定是否已经回答。我建议您在收到响应后使用流缓存。这意味着,from("servlet:/receive").streamCaching()在您的路线中使用。