有一个包含多个可能的 Rest-responces 的 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();处理器执行一些验证/业务逻辑。大致看起来像:@Overridepublic 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() 的处理器
1 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
找到了解决办法。如果发生错误,看起来 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;
}
添加回答
举报
0/150
提交
取消