我目前正在使用 Apache Camel(使用 camel-spring)开发 REST 应用程序,并出现一些令人困惑的行为。我在 REST DSL 中定义了一组端点,其中一些只是对另一台服务器的代理请求,而另一些则传递到我为数据聚合定义的路由。 DSL 如下所示:
<rest>
<!-- Specific DSL requests -->
<get uri="/v1/aggregate/data" consumes="application/json" produces="application/json">
<to uri="direct:dataEnrichment" />
</get>
<get uri="/v1/customer/{custId}/devices" consumes="application/json" produces="application/json">
<to uri="direct:getCustomerDevices" />
</get>
<get uri="/v1/login/{custId}" consumes="application/json" produces="application/json">
<to uri="direct:getCustomer" />
</get>
<get uri="/v1/status" consumes="application/json" produces="application/json">
<to uri="direct:statusInfo" />
</get>
<!-- Proxy requests -->
<post uri="/v1?matchOnUriPrefix=true&chunked=false">
<to uri="direct:proxyOut" />
</post>
<get uri="/v1?matchOnUriPrefix=true&chunked=false">
<to uri="direct:proxyOut" />
</get>
<put uri="/v1?matchOnUriPrefix=true&chunked=false">
<to uri="direct:proxyOut" />
</put>
<delete uri="/v1?matchOnUriPrefix=true&chunked=false">
<to uri="direct:proxyOut" />
</delete>
</rest>
这个想法是,任何没有完全匹配 URI 的请求都会被代理到另一个系统(不是 Apache Camel)。这使我不必为另一个系统上的每个 REST API 编写定义(有很多)。
一切都运行良好,直到我在 URI 中添加了带有 {custId} 的两个请求。这些请求工作正常,但每次我尝试应该通过代理的 URI 时,我都会收到 405 Method Not Allowed。
编辑: 我还应该提到我正在使用 Jetty 作为 REST 组件。 Camel 独立运行,使用 org.apache.camel.spring.Main 启动它。我在这个阶段用 Postman 调用它,405 响应似乎来自 Jetty/Camel。
REST 配置如下所示(安全处理程序使用 Jetty BasicAuthenticator):
<restConfiguration contextPath="/mobilegateway/api" scheme="http"
host="0.0.0.0" bindingMode="off"
enableCORS="true"
component="jetty" port="8079">
<endpointProperty key="handlers" value="securityHandler" />
<endpointProperty key="sessionSupport" value="true" />
<endpointProperty key="httpClient.idleTimeout" value="30000" />
</restConfiguration>
Proxy 请求全部发送到 direct:proxyOut 路由,如下所示:
<route id="proxyOutbound">
<description>A simple outbound route to proxy REST requests.</description>
<from uri="direct:proxyOut" />
<removeHeaders pattern="Authorization" />
<to
uri="http://{{remoteAddress}}/data/json?bridgeEndpoint=true&throwExceptionOnFailure=false" />
</route>
目的是在代理请求中传递 URI 中/v1 之后的所有内容。我已经使用 Wireshark 检查过,请求没有被代理。如果我删除路径中带有 {custId} 的路由,一切正常。
我是做错了什么还是 camel/camel-spring 中的错误?
最佳答案
Camel 虫,详情可以在这里找到:
关于spring - 不允许使用 Apache Camel REST DSL 405 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46936348/