我有一个像这样的 Camel 路线定义:
@Component
public class AggregateRouter extends AbstractRouteBuilder {
@Override
public void configure() throws Exception {
super.configure();
from("{{endpoint.users}}/{id}?matchOnUriPrefix=true")
.to("bean:routeUtils?method=validateQueryParams"))
.to("bean:routeUtils?method=loadRouteProperties"))
.to("{{uri.api.users}}")
.unmarshal().json(JsonLibrary.Jackson, Map.class)
.to("bean:routeUtils?method=extractAndAddToProperty"))
.to("bean:routeUtils?method=prepareAggregateRestCalls"))
.multicast()
.stopOnException()
.to("seda:operation1")
.to("seda:operation2")
.end()
.setBody(simple("${property.result}"))
.marshal().json(JsonLibrary.Jackson)
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"));
from("seda:operation2")
.toD("{{uri.api.users.operation2}}")
.unmarshal()
.json(JsonLibrary.Jackson, List.class)
.to("bean:userService?method=addOp2"));
from("seda:operation1")
.toD("{{uri.api.users.operation1}}")
.choice()
.when(AbstractHelper::isOk)
.unmarshal()
.json(JsonLibrary.Jackson, List.class)
.to("bean:userService?method=addOp1"))
.otherwise()
.unmarshal()
.json(JsonLibrary.Jackson, Map.class)
.to("bean:userService?method=handleRouteSubscriptionException"))
.end();
}
}
我希望仅当 HTTP 请求作为 GET
请求进入集成层时才能使用此定义。现在的问题是:我还有两个操作(PUT
和DELETE
),但我不希望对这些操作进行“特殊”处理两个(至少现在)...并且它们的行为与 GET
一样,因为此路由定义是“拦截”并处理请求。
我无法使用Rest DSL(该项目目前是这样的)。我还尝试使用 &httpMethodRestrict
像 {{endpoint.users}}/{id}?matchOnUriPrefix=true&httpMethodRestrict=PUT
但它也不起作用。
有什么线索吗?
最佳答案
我也认为 httpMethodRestrict 是可行的方法。文档对参数的描述相当模糊...尝试像 httpMethodRestrict=GET 一样使用它(阅读:限制对 GET 的请求)
另一种可能的解决方案可能是使用 header 信息Exchange.HTTP_METHOD,例如.filter(header("Exchange.HTTP_METHOD").isEqualTo("GET")) -只是为了得到这个想法(我没有尝试过)
关于java - 将 Camel 路由限制为 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515014/