java使用ApacheCamel调用外部服务给httpoperationfailedexception?

0dxa2lsx  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(252)

服务路径.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <routeContext id="servicesCommonRoutes"
        xmlns="http://camel.apache.org/schema/spring">

        <route id="Route">
            <from uri="direct:route" />
            <marshal>
                <custom ref="Request" />
            </marshal>
            <setHeader name="Content-Type" inheritErrorHandler="true">
                <constant>application/json</constant>
            </setHeader>
            <setHeader name="Exchange.HTTP_METHOD">
                <constant>POST</constant>
            </setHeader>
            <to uri="{{svrRouteEndpoint}}" />
            <unmarshal>
                <custom ref="Response" />
            </unmarshal>
        </route>

    </routeContext>
</beans>

应用程序属性

svrRouteEndpoint=http://somehost.com/endpoint
svrRoute=direct:route
svrHost=host.com
svrApiKey=abc123

服务.java

@Autowired
    private ProducerTemplate template;

    @Value("${svrHost}")
    private String svrHost;

    @Value("${svrApiKey}")
    private String svrApiKey;

public Response process(Request req, Map<String, Object> headers) throws CamelExecutionException {
Response response = template.requestBodyAndHeaders("direct:route", req, headers, Response.class);
return response;
}

 public Map<String, Object> createHeaders(String xWuExternalrefid) {
    Map<String, Object> headers = new HashMap<>();\
    headers.put("host", svrHost);
    headers.put("x-api-key", svrApiKey);
    return headers;
}

现在服务api合同要求只提供3个头和请求体x-api-key,主机和内容类型作为头和requestbody,所以在调用process方法之前,我要在map中包含所有3个头和正确的键值对,同样地创建请求pojo,现在在process方法中调用requestbodyandheaders,它返回原因为httpoperationfailedexception的camelexecutionexception,返回消息:

Exception with the message: {"message": "Forbidden"} and status code : 403.

当我直接从postman中点击服务时,使用完全相同的header值和相同的请求,我得到的响应状态为200ok,但当我从服务中测试它,然后使用camel调用外部服务时就不是了。
无法找出问题的真正原因。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题