用于RESTful Web服务公开的Camel最佳实践

时间:2013-06-26 11:38:10

标签: rest apache-camel

我想使用camel公开RESTfull Web服务。我使用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;
    }
}

1 个答案:

答案 0 :(得分: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>
相关问题