【问题标题】:Camel best practices for RESTfull web service exposingCamel RESTful Web 服务公开的最佳实践
【发布时间】:2013-06-26 11:38:10
【问题描述】:

我想使用骆驼公开一个 RESTfull 网络服务。我使用 URI 模板来定义我的服务合同。我想知道如何根据 URI 模板将请求路由到我的 ServiceProcessor 的相关方法。

以如下两个操作为例:

        @GET
        @Path("/customers/{customerId}/")
        public Customer loadCustomer(@PathParam("customerId") final String customerId){
            return null;
        }

        @GET
        @Path("/customers/{customerId}/accounts/")
        List<Account> loadAccountsForCustomer(@PathParam("customerId") final String customerId){
            return null;
        }

以下是我使用的路线:

<osgi:camelContext xmlns="http://camel.apache.org/schema/spring">
    <route trace="true" id="PaymentService">
        <from uri="`enter code here`cxfrs://bean://customerCareServer" />
        <process ref="customerCareProcessor" />
    </route>
</osgi:camelContext>

有什么方法可以将 uri 标头 (Exchange.HTTP_PATH) 匹配到我的服务定义中的现有模板 uri?

以下是我的服务的服务合同:

@Path("/customercare/")
public class CustomerCareService {

    @POST
    @Path("/customers/")
    public void registerCustomer(final Customer customer){

    }

    @POST
    @Path("/customers/{customerId}/accounts/")
    public void registerAccount(@PathParam("customerId") final String customerId, final Account account){

    }

    @PUT
    @Path("/customers/")
    public void updateCustomer(final Customer customer){

    }

    @PUT
    @Path("/accounts/")
    public void updateAccount(final Account account){

    }

    @GET
    @Path("/customers/{customerId}/")
    public Customer loadCustomer(@PathParam("customerId") final String customerId){
        return null;
    }

    @GET
    @Path("/customers/{customerId}/accounts/")
    List<Account> loadAccountsForCustomer(@PathParam("customerId") final String customerId){
        return null;
    }

    @GET
    @Path("/accounts/{accountNumber}")
    Account loadAccount(@PathParam("accountNumber") final String accountNumber){
        return null;
    }
}

【问题讨论】:

    标签: rest apache-camel


    【解决方案1】:

    cxfrs 端点消费者提供一个特殊的交换标头 operationName,其中包含服务方法名称(registerCustomerregisterAccount 等)。

    您可以使用此标头决定如何处理请求,如下所示:

    <from uri="cxfrs://bean://customerCareServer" />
    <choice>
        <when>
            <simple>${header.operationName} == 'registerCustomer'</simple>
            <!-- registerCustomer request processing -->
        </when>
        <when>
            <simple>${header.operationName} == 'registerAccount'</simple>
            <!-- registerAccount request processing -->
        </when>
        ....
    </choice>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2014-07-07
      • 2011-08-06
      相关资源
      最近更新 更多