【问题标题】:Changing Camel REST HTTP Binding Configuration更改 Camel REST HTTP 绑定配置
【发布时间】:2020-06-19 13:17:47
【问题描述】:

我正在使用 Camel REST 和 Camel servlet 处理 REST 传输,并希望更改在 HTTP 请求和响应中处理来自交换的标头的方式。我正在使用 Spring XML 来配置我的应用程序。这是我正在使用的相关配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://camel.apache.org/schema/spring
            http://camel.apache.org/schema/spring/camel-spring.xsd">
    <!-- Custom Camel Configuration //////////////////////////////////////////////////////////////////////////////// -->
    <bean id="myHttpBinding" class="com.example.MyHttpBinding"/>
    <bean id="servlet"       class="org.apache.camel.component.servlet.ServletComponent">
        <property name="httpBinding" ref="myHttpBinding"/>
    </bean>

    <!-- Routes //////////////////////////////////////////////////////////////////////////////////////////////////// -->
    <camel:camelContext id="myCamelContext">
        <camel:contextScan/>

        <camel:restConfiguration component="servlet" enableCORS="true" bindingMode="json" skipBindingOnErrorCode="false">
            <camel:corsHeaders key="Access-Control-Allow-Methods" value="PATCH, PUT, POST, DELETE, GET, OPTIONS, HEAD"/>
            <camel:corsHeaders key="Access-Control-Allow-Origin"  value="*"/>
            <camel:corsHeaders key="Access-Control-Allow-Headers" value="*"/>
        </camel:restConfiguration>
    </camel:camelContext>
</beans>

创建路由后,我看到端点配置了MyHttpBinding 集。但是,传入的请求仍在使用ServletRestHttpBinding。这是因为当 Camel 创建消费者时,它会执行这段代码:

if (!map.containsKey("httpBinding")) {
    // use the rest binding, if not using a custom http binding
    HttpBinding binding = new ServletRestHttpBinding();
    binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy());
    binding.setTransferException(endpoint.isTransferException());
    binding.setEagerCheckContentAvailable(endpoint.isEagerCheckContentAvailable());
    endpoint.setHttpBinding(binding);
}

如何以 Camel 尊重的方式设置 HTTP 绑定?

【问题讨论】:

    标签: java spring apache-camel camel-rest


    【解决方案1】:

    因为 Camel 最终会在端点上查找 httpBinding 属性以确定要使用的绑定策略,因此必须配置 REST 组件以将该属性添加到端点,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:camel="http://camel.apache.org/schema/spring"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://camel.apache.org/schema/spring
                http://camel.apache.org/schema/spring/camel-spring.xsd">
        <!-- Custom Camel Configuration //////////////////////////////////////////////////////////////////////////////// -->
        <bean id="myHttpBinding" class="com.example.MyHttpBinding"/>
    
        <!-- Routes //////////////////////////////////////////////////////////////////////////////////////////////////// -->
        <camel:camelContext id="myCamelContext">
            <camel:contextScan/>
    
            <camel:restConfiguration component="servlet" enableCORS="true" bindingMode="json" skipBindingOnErrorCode="false">
                <camel:endpointProperty key="httpBinding"                  value="myHttpBinding"/>
                <camel:corsHeaders      key="Access-Control-Allow-Methods" value="PATCH, PUT, POST, DELETE, GET, OPTIONS, HEAD"/>
                <camel:corsHeaders      key="Access-Control-Allow-Origin"  value="*"/>
                <camel:corsHeaders      key="Access-Control-Allow-Headers" value="*"/>
            </camel:restConfiguration>
        </camel:camelContext>
    </beans>
    

    请注意,我删除了自定义 servlet 组件,因为它不是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 2015-07-18
      • 2011-06-23
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      相关资源
      最近更新 更多