代码之家  ›  专栏  ›  技术社区  ›  Ermintar

当HttpCode时Camel Rest resonse body为空!=200个

  •  1
  • Ermintar  · 技术社区  · 6 年前

    有一个rest路由有多个可能的rest responce(它们属于不同的类型,但假设它只是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);
    }
    

    阳性病例执行结果:

    代码详细信息 响应体 { }

    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()的处理器

    @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” }

    代码详细信息 响应体 无法分析JSON。原始结果:

    MyResponse类{ 收件人:123456

    (收到的原始数据)

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

    {"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

    这是骆驼虫吗?如果是的话,怎么解决? 有办法吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Ermintar    6 年前

    找到了解决办法。看起来Camel默认路由配置防止在发生错误时封送正文。即使手动声明需要在.responseModel(MyClass.class)中进行封送处理

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