java - 将 Camel 路由限制为 HTTP 方法

标签 java apache-camel

我有一个像这样的 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 请求进入集成层时才能使用此定义。现在的问题是:我还有两个操作(PUTDELETE),但我不希望对这些操作进行“特殊”处理两个(至少现在)...并且它们的行为与 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/

相关文章:

java - 如何在页面上添加图片

grails - 使用路由1.2.2插件时,Grails无法解析Apache Camel组件(当需要.jar时需要.zip)

java - Camel 动态模拟端点

java - 在java中的二维字符数组中查找单词。如果某些部分没有被注释掉,为什么我会遇到搜索问题?

validation - Camel 验证错误消息

java - Apache Camel Java DSL 向正文添加换行符

java - 如何使用 Apache Camel + SSL 接收电子邮件?

java - 如何使用内容类型 XML 和查询参数触发 HTTP post?

javascript - 无法启动模拟器。原因: No emulators found as an output of `emulator -list-avds` .,安装应用程序失败

java - 这种 zip 方法有什么问题吗?