gpt4 book ai didi

java - 当 Http Code!=200 时,Camel Rest 响应主体为空

转载 作者:可可西里 更新时间:2023-11-01 16:59:16 27 4
gpt4 key购买 nike

有一个具有多个可能的 Rest-responses 的 rest-route(它们具有不同的类型,但我们假设它只是 MyResponse.class)。

rest().post("/{{camel.rest.version}}/myjson")
.consumes("application/json").produces("application/json")
.type(SampleRequest.class)
.responseMessage().code("200").responseModel(MyResponse.class).endResponseMessage()
.responseMessage().code("400").responseModel(MyResponse.class).endResponseMessage()
.responseMessage().code("500").responseModel(MyResponse.class).endResponseMessage()
.route().routeId("rest_myroute")
.log(LoggingLevel.INFO, "API_REQ Recieved http request ${body}")
.process(sampleProcessor).id("sample_transform")
.log(LoggingLevel.INFO, "API_RESP Response ${body}")
.endRest();

处理器执行一些验证/业务逻辑。大致看起来像:

@Override
public void process(Exchange exchange) throws Exception {
SampleRequest inReq = exchange.getIn().getBody(SampleRequest.class);
if (inReq.getMsgMode().equals("500"))
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.SC_INTERNAL_SERVER_ERROR);
if (inReq.getMsgMode().equals("400"))
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.SC_BAD_REQUEST);
MyResponse myClass = new MyResponse();
myClass.setRecId("123456");
exchange.getOut().setBody(myClass);
}

正例的执行结果:

代码细节200响应体{ “recId”:“123456”

500 或 400 箱代码详情400 错误:错误请求响应头连接:保持 Activity 状态

(没有正文)

如果我将路由更改为手动编码:

rest().post("/{{camel.rest.version}}/myjson")
.consumes("application/json").produces("application/json")
.type(SampleRequest.class)
.route().routeId("rest_myroute")
.log(LoggingLevel.INFO, LoggingConst.API_REQ+" Recieved http request ${body}")
.process(sampleProcessor).id("sample_transform")
.marshal().json(JsonLibrary.Jackson)
.log(LoggingLevel.INFO, LoggingConst.API_RESP+" Response ${body}")
.endRest();

结果是一样的。

好的。以防万一使用 getOut() 不好,让我们试试 getIn()。处理器使用 getIn()

@Override
public void process(Exchange exchange) throws Exception {
SampleRequest inReq = exchange.getIn().getBody(SampleRequest.class);
if (inReq.getMsgMode().equals("500"))
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.SC_INTERNAL_SERVER_ERROR);
if (inReq.getMsgMode().equals("400"))
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.SC_BAD_REQUEST);
MyResponse myClass = new MyResponse();
myClass.setRecId("123456");
exchange.getIn().setBody(myClass);
}

结果:

正面案例:

代码细节200响应体"eyJyZWNJZCI6IjEyMzQ1NiJ9"

(双编码完成)

否定案例:

400 错误:错误请求响应体{ “recId”:“123456”

让我们完全删除编码!

rest().post("/{{camel.rest.version}}/myjson")
.consumes("application/json").produces("application/json")
.type(SampleRequest.class)
.route().routeId("rest_myroute")
.log(LoggingLevel.INFO, LoggingConst.API_REQ+" Recieved http request ${body}")
.process(sampleProcessor).id("sample_transform")/*.streamCaching()*/
.log(LoggingLevel.INFO, LoggingConst.API_RESP+" Response ${body}")
.endRest();

积极的结果:

代码细节200响应体{ “recId”:“123456”

否定结果:

代码细节400 错误:错误请求响应体无法解析 JSON。原始结果:

类我的响应{ 记录:123456

(收到原始数据)

在所有情况下,我的 API 日志在 Camel 准备休息响应之前显示相同的结果:

{"timestamp":"2018-08-21T16:04:37.284+00:00","level":"INFO","logger_name":"rest_myroute","message":"ApiRq Recieved http request SampleRequest(msgMode=400)","traceId":"-4589659693669010018","spanId":"-3263510332551481190","camel.exchangeId":"ID-VRN26-1534867331800-0-3","camel.contextId":"IndividualClientService","camel.messageId":"ID-VRN26-1534867331800-0-4","camel.breadcrumbId":"ID-VRN26-1534867331800-0-3","camel.routeId":"rest_myroute","parentId":"-4589659693669010018"}
{"timestamp":"2018-08-21T16:04:37.288+00:00","level":"INFO","logger_name":"rest_myroute","message":"ApiRsp Response {\"recId\":\"123456\"}","traceId":"-4589659693669010018","spanId":"-3263510332551481190","camel.exchangeId":"ID-VRN26-1534867331800-0-3","camel.contextId":"IndividualClientService","camel.messageId":"ID-VRN26-1534867331800-0-4","camel.breadcrumbId":"ID-VRN26-1534867331800-0-3","camel.routeId":"rest_myroute","parentId":"-4589659693669010018"}

Camel 版本:2.21.0.000033-fuse-000001-redhat-1

这是 Camel 虫吗?如果是这样,它修复了什么?任何解决方法?

最佳答案

找到解决方案。看起来 Camel 默认路由配置会在发生错误时阻止编码主体。即使您手动声明您需要在 .responseModel(MyClass.class) 中编码

设置skipBindingOnErrorCode(false)

@Bean
RouteBuilder restConfiguration(){

RouteBuilder restConfiguration = new RouteBuilder() {
@Override
public void configure() throws Exception{

restConfiguration().component("servlet")
.bindingMode(RestBindingMode.json)
.skipBindingOnErrorCode(false) //!!!!!!!!! add this
.contextPath(apiContextPath);
}
};
return restConfiguration;
}

关于java - 当 Http Code!=200 时,Camel Rest 响应主体为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51952864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com