更改骆驼REST HTTP绑定配置

时间:2020-06-19 13:17:47

标签: java spring apache-camel camel-rest

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

protected override async void OnStartup(StartupEventArgs e)
    {
        await host.StartAsync();

        var mainWindow = host.Services.GetRequiredService<MainWindow>();
        mainWindow.Show();

        base.OnStartup(e);
    }

创建路由后,我看到端点配置为<?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。这是因为当Camel创建使用者时,它将执行以下代码块:

ServletRestHttpBinding

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

1 个答案:

答案 0 :(得分:1)

由于骆驼最终在端点上寻找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组件,因为它不是必需的。