Java HttpCode时驼峰休息响应体为空=200
有一个rest路由,其中包含多个可能的rest响应(它们属于不同的类型,但假设它只是MyResponse.class) 处理器执行一些验证/业务逻辑。大致上看起来:Java HttpCode时驼峰休息响应体为空=200,java,http,apache-camel,Java,Http,Apache Camel,有一个rest路由,其中包含多个可能的rest响应(它们属于不同的类型,但假设它只是MyResponse.class) 处理器执行一些验证/业务逻辑。大致上看起来: @Override public void process(Exchange exchange) throws Exception { SampleRequest inReq = exchange.getIn().getBody(SampleRequest.class); if (inReq.getMsgMode()
@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错误:请求错误
响应头
连接:保持活力
(无正文)
如果我将路线更改为手动编组:
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()的处理器
结果:
阳性病例:
代码详细信息
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。原始结果:
类MyResponse{
累西德:123456
}
(接收到的原始数据)
在所有情况下,我的API日志在camel准备rest响应之前显示相同的结果:
{"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"}
驼色版本:2.21.0.000033-fuse-000001-redhat-1
这是骆驼虫吗?如果是的话,它是怎么修复的?
有解决办法吗?找到了解决方案。看起来驼峰默认路由配置会在发生错误时阻止编组实体。即使手动声明需要在.responseModel(MyClass.class)中编组 设置SkipBindingErrorCode(错误)
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();
{"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"}
@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;
}