【问题标题】:Path variables in Apache Camel rest routeApache Camel 休息路线中的路径变量
【发布时间】:2021-05-27 14:48:46
【问题描述】:

我想通过以下方式在我的 REST 路由 URL 中使用路径变量:

rest().post("/companies/{companyId}/branches/branchId={branchId}");

companyId 可以作为标题正确检索,但是,“branchId={branchId}”被视为文字字符串。因此请求:
/companies/100/branches/branchId=200 - 将返回 404 not found。
但是
/companies/100/branches/branchId={branchId} - 会进入路线。

我想使用 branchId 作为标头,与 companyId 一样,无需更改 URL 的结构。

【问题讨论】:

    标签: rest web-services apache-camel


    【解决方案1】:

    每个路径或查询参数都可以通过 Apache Camel 中的标头接收。您可以使用简单的表达式检索标题;

    ${header.your-header}

    所以你可以检索如下参数;

    parameter simple expression
    companyId ${header.companyId}
    branchId ${header.branchId}

    这是我为您制作的有效路线示例;

    package com.bzdgn.camelso.route;
    
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.model.rest.RestBindingMode;
    
    public class RestEndpointRoute extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            rest()
                .post("/companies/{companyId}/branches/{branchId}")
                .consumes("text/plain")
                .produces("text/plain")
                .to("direct:handle-post");
            
            from("direct:handle-post")
                .id("post-route")
                .log("Received Body: ${body}")
                .log("Company Id: ${header.companyId}")
                .log("Branch  Id: ${header.branchId}")
                .setBody(simple("CompanyId = ${header.companyId} \nBranchId  = ${header.branchId}"))
                .convertBodyTo(String.class);
            
            restConfiguration()
                .component("netty-http")
                .host("localhost")
                .port("8080")
                .bindingMode(RestBindingMode.auto);
        }
    
    }
    

    如果您向指定的 URL 发送 HTTP Post 请求,日志将如下所示;

    2021-05-28 21:13:30 INFO  HttpServerBootstrapFactory:53 - BootstrapFactory on port 8080 is using bootstrap configuration: [NettyServerBootstrapConfiguration{protocol='http', host='localhost', port=8080, broadcast=false, sendBufferSize=65536, receiveBufferSize=65536, receiveBufferSizePredictor=0, workerCount=0, bossCount=1, keepAlive=true, tcpNoDelay=true, reuseAddress=true, connectTimeout=10000, backlog=0, serverInitializerFactory=org.apache.camel.component.netty.http.HttpServerInitializerFactory@ae3540e, nettyServerBootstrapFactory=null, options=null, ssl=false, sslHandler=null, sslContextParameters='null', needClientAuth=false, enabledProtocols='TLSv1,TLSv1.1,TLSv1.2, keyStoreFile=null, trustStoreFile=null, keyStoreResource='null', trustStoreResource='null', keyStoreFormat='JKS', securityProvider='SunX509', passphrase='null', bossGroup=null, workerGroup=null, networkInterface='null', reconnect='true', reconnectInterval='10000'}]
    2021-05-28 21:13:30 INFO  NettyComponent:164 - Creating shared NettyConsumerExecutorGroup with 17 threads
    2021-05-28 21:13:31 INFO  SingleTCPNettyServerBootstrapFactory:182 - ServerBootstrap binding to localhost:8080
    2021-05-28 21:13:33 INFO  NettyConsumer:77 - Netty consumer bound to: localhost:8080
    2021-05-28 21:13:33 INFO  AbstractCamelContext:2983 - Routes startup summary (total:2 started:2)
    2021-05-28 21:13:33 INFO  AbstractCamelContext:2988 -     Started post-route (direct://handle-post)
    2021-05-28 21:13:33 INFO  AbstractCamelContext:2988 -     Started route1 (rest://post:/companies/%7BcompanyId%7D/branches/%7BbranchId%7D)
    2021-05-28 21:13:33 INFO  AbstractCamelContext:3000 - Apache Camel 3.10.0 (camel-1) started in 3s337ms (build:93ms init:274ms start:2s970ms)
    2021-05-28 21:13:35 INFO  post-route:166 - Received Body: Example text body here
    2021-05-28 21:13:35 INFO  post-route:166 - Company Id: my-custom-company
    2021-05-28 21:13:35 INFO  post-route:166 - Branch  Id: my-custom-branch-id
    
    

    您可以像这样通过邮递员对其进行测试。请求,以及下图中列出的标头,不要忘记添加 “Content Type: text/plain”,因为这是在它将使用并生成“text/plain”的路由中定义的”。

    【讨论】:

    • 感谢您的回答。但是,它错过了这个问题的主要目的,即:如何保持给定的结构,尤其是 /branchId={branchId} 部分。在您的回答中,您省略了 "branchId=" 部分。
    • @mar3g 你是对的,但这是关于在 URI 中使用特殊字符。我尝试使用 %3D 而不是等号,但它仍然返回 404。我会在轮班完成后重新开始工作;)
    猜你喜欢
    • 2016-01-05
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多