2013-06-26 46 views
1

我想使用駱駝公開一個RESTfull web服務。我已經使用URI模板來定義我的服務合約。我想知道如何根據URI模板將請求路由到我的ServiceProcessor的相關方法。RESTfull Web服務公開的駱駝最佳實踐

作爲一個例子,採取以下兩種操作:

 @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

cxfrs終端消費者提供了包含一個服務方法名(registerCustomerregisterAccount等)的特殊交換頂部operationName

你可以決定如何使用這個標題好像在與一個要求做到:

<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> 
+0

感謝亞歷山大! – Dilunika