Apache Camel - 在向路由发送消息后的响应

时间:2016-02-24 16:11:32

标签: java json spring-boot apache-camel


我在项目中使用Apache CamelSpring 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");

我想知道为什么会这样,以及它与杰克逊序列化有什么关系。如果我不想发送第一个请求的回复怎么办?我红了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");

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

编辑这是我的用户类:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

不确定它是否已经回答。我建议您在收到回复后使用Stream Caching。这意味着,在您的路线中使用Type "it" for more