骆驼API在HTTP请求中抛出HttpOperationFailedException

时间:2019-09-05 17:34:59

标签: java spring apache-camel spring-camel camel-http

我有一个骆驼API,其端点通过http调用了API的另一个实例(在另一个上下文中)。该请求使用POST方法,并在正文中包含一个字符串列表。正文中的列表包含一个String键,该键指示下一个实例被调用。 在调用下一个实例之前,将主体转换为String。

调用后,第二个实例返回“ org.apache.camel.http.common.HttpOperationFailedException”,并显示消息“ HTTP操作无法调用状态代码为500的http://localhost:8081/v1/test/context”。

在API的seconf实例中进行调试,通过onException方法捕获请求。该请求永远不会进入路由。

我验证了标头包含HttpMethod POST。还要确认List是在exchange-> in-> body中转换为String的。

public void configure() throws Exception{

        restConfiguration()
                .apiProperty("api.title", "api-test-context")
                .apiProperty("api.version", "0.0.1-SNAPSHOT")
                .apiProperty("base.path", "v1/")
                .apiProperty("api.path", "/")
                .component("servlet")
                .bindingMode(RestBindingMode.json)
                .skipBindingOnErrorCode(false);

        rest("/test")
            .post("/context").consumes(MediaType.APPLICATION_JSON_VALUE)
                .description("Propagate context information")
                .type(Object.class).outType(Object.class)
                .route().routeId("jaegerInformationRoute")
                .to("direct:contextInformation")
            .endRest();

        from("direct:contextInformation")
            .log("[API-TEST-CONTEXT] - Processing request...")
            .process(exchange -> {
                modelMapper = new ModelMapper();
                Object stepListRaw =  exchange.getIn().getBody();
                Type listType = new TypeToken<ArrayList<String>>() {}.getType();
                List<String> stepList = modelMapper.map(stepListRaw, listType);
                exchange.setProperty("NEXT_STEP", stepList.get(0));
                stepList.remove(0);
                Object objList = modelMapper.map(stepList,Object.class);
                exchange.getIn().setBody(objList.toString());  /** Body object to String*/
            })
            .log("[API-TEST-CONTEXT] - Verifying next step...")
            .choice()
                .when(exchangeProperty("NEXT_STEP").isEqualTo("next"))
                    .log("[API-TEST-CONTEXT] - Next step is http://localhost:8081")
                    .to("http://localhost:8081/v1/test/context?bridgeEndpoint=true")
                .endChoice()
                .when(exchangeProperty("NEXT_STEP").isEqualTo("self"))
                    .log("[API-TEST-CONTEXT] - Next step is http://localhost:8080")
                    .to("http://localhost:8080/v1/test/context?bridgeEndpoint=true")
                .endChoice()
                .otherwise()
                    .log("[API-TEST-CONTEXT] - There aren't more steps...")
                    .to("direct:generateGlobalResponse")
            .log("[API-TEST-CONTEXT] - Processing information of last steps...")
            .to("direct:generateGlobalResponse")
        .end();

        from("direct:generateGlobalResponse")
            .log("[API-TEST-CONTEXT] - Generating REST response.")
            .process(exchange -> {
                Response response = ResponseBuilder.init()
                    .withMetaMethod((String) exchange.getIn().getHeader("CamelHttpMethod"))
                    .withMetaOperation((String) exchange.getIn().getHeader("CamelHttpUri"))
                    .addData(exchange.getIn().getBody())
                    .build();
                exchange.getOut().setBody(response);
            }).end();

    }

我是否很好地处理了http请求?

我希望请求正文中的字符串列表到达API的第二个实例。

我认为问题在于请求的主体。

0 个答案:

没有答案