Java 发送Camel HTTP Post自定义正文

Java 发送Camel HTTP Post自定义正文,java,http,spring-boot,apache-camel,Java,Http,Spring Boot,Apache Camel,我是新来的阿帕奇骆驼。我正在尝试创建调用多个RESTAPI的路由,并将响应聚合为一个。 但是由于某些原因,我正在创建的JSON请求没有到达rest端点。 在调试期间,我看到Exchange对象确实具有我设置的值,并将其转换为字节数组,另一方面,RESTAPI接收空对象 我正在从事一个Spring boot项目,我尝试了不同的方法将请求编组到JSON,包括Gson和Jackson。这些似乎都不管用 请帮忙 from("direct:oneResponse") .multicast(

我是新来的阿帕奇骆驼。我正在尝试创建调用多个RESTAPI的路由,并将响应聚合为一个。 但是由于某些原因,我正在创建的JSON请求没有到达rest端点。 在调试期间,我看到Exchange对象确实具有我设置的值,并将其转换为字节数组,另一方面,RESTAPI接收空对象

我正在从事一个Spring boot项目,我尝试了不同的方法将请求编组到JSON,包括Gson和Jackson。这些似乎都不管用

请帮忙

from("direct:oneResponse")
        .multicast(new MyAggregationStrategy()).parallelProcessing()
        .to("direct:rest1call", "direct:rest2call")
        .end();


from("direct:rest1call")
        .routeId("rest1call")
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Content-Type", constant("application/json"))
        .setHeader("Accept", constant("application/json"))
        .process(new Processor() {              
            @Override
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setBody(<<valid json>>); //json values as required for the rest call.
            }
        })
        .to("http4://localhost:5555/mock/rest1call")
        .setProperty("route", simple("routeId"))
        .unmarshal(new JacksonDataFormat(Rest1Response.class));

from("direct:rest2call")
        .routeId("rest2call")
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Content-Type", constant("application/json"))
        .setHeader("Accept", constant("application/json"))
        .process(new Processor() {              
            @Override
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setBody(<<valid json>>); //json values as required for the rest call.
            }
        })
        .to("http4://localhost:5555/mock/rest2call")
        .setProperty("route", simple("routeId"))
        .unmarshal(new JacksonDataFormat(Rest2Response.class));
from(“直接:oneResponse”)
.multicast(新的MyAggregationStrategy()).parallelProcessing()
。至(“直接:REST1调用”、“直接:REST2调用”)
.end();
from(“直接:rest1call”)
.routeId(“rest1call”)
.setHeader(Exchange.HTTP_方法,常量(“POST”))
.setHeader(“内容类型”,常量(“应用程序/json”))
.setHeader(“接受”,常量(“应用程序/json”))
.process(新处理器(){
@凌驾
公共作废进程(Exchange)引发异常{
exchange.getIn().setBody();//rest调用所需的json值。
}
})
.to(“http4://localhost:5555/mock/rest1call”)
.setProperty(“路由”,简单(“路由ID”))
.unmarshal(新JacksonDataFormat(rest1响应类));
from(“直接:rest2call”)
.routeId(“rest2call”)
.setHeader(Exchange.HTTP_方法,常量(“POST”))
.setHeader(“内容类型”,常量(“应用程序/json”))
.setHeader(“接受”,常量(“应用程序/json”))
.process(新处理器(){
@凌驾
公共作废进程(Exchange)引发异常{
exchange.getIn().setBody();//rest调用所需的json值。
}
})
.to(“http4://localhost:5555/mock/rest2call”)
.setProperty(“路由”,简单(“路由ID”))
.unmarshal(新JacksonDataFormat(Rest2Response.class));

您能否尝试创建一个处理器并指定其中的所有标题和正文

.process(new Processor() {              
            @Override
            public void process(Exchange exchange) throws Exception {
                exchange.getOut().setHeader(Exchange.HTTP_METHOD, HttpMethod.POST);
                exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");
                exchange.getOut().setHeader("Accept", "application/json");
                /* this is one way, string representation of json, but maybe you can try to build Model and fill that model with data */
                exchange.getIn().setBody(<<valid json>>); //json values as required for the rest call.
            }
        })

试试GsonDataFormat,它对我很有用。

我觉得你的东西还不错。你的名字仅仅是一个字符串吗?您能否将RESTAPI更改为只接收字符串并检查它是否从exchange获取值?如果您添加一个.log(${body}),只需为.to(“http…)添加即可。您将看到它将发送什么。感谢您的帮助。是的。这只是一个包含有效json的字符串,在我添加.log(${body})时会打印出来。将rest API更改为接收字符串,但当我运行该程序时,它接收空值。我使用的是2.22.2版,它对我正常工作。抱歉,我看不出您的路由有什么问题。
.marshal(yourDataFormat)