2014-03-03 11 views
1

例子:如何在Java(REST api)中指定2個資源位於同一路徑中,因此執行取決於客戶端提供的版本?

@POST 
@Path("/commonPath") 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.APPLICATION_XML) 
public Response getMethoV1(RequestDto1 reqDto) { 
    //logic 
} 

@POST 
@Path("/commonPath") 
@Consumes(MediaType.APPLICATION_XML) 
@Produces(MediaType.APPLICATION_XML) 
public Response getMethoV2(RequestDto2 reqDto) { 
    //logic 
} 

如何可以將此代碼進行調整,所以誰也切換到2版本可以使用的資源getMethod2誰使用的應用程序版本1可以去getMethod1(客戶端)和那些()? RequestDtos可以是也可以不是相同的類對象。

回答

1

版本API的最簡單方法是在開發過程中將版本號作爲URI的一部分。

/commonPath/v1.0
/commonPath/v1.1

那麼您的代碼將變成這個樣子

 @GET 
     @Path("/commonPath/v1.0") 
     @Consumes(MediaType.APPLICATION_XML) 
     @Produces(MediaType.APPLICATION_XML) 
     public Response getMethoV1(RequestDto1 reqDto) { 
      //logic 
     } 

     @GET 
     @Path("/commonPath/v1.1") 
     @Consumes(MediaType.APPLICATION_XML) 
     @Produces(MediaType.APPLICATION_XML) 
     public Response getMethoV2(RequestDto2 reqDto) { 
      //logic 
     } 
+0

是的,但我已閱讀,版本信息,而應是作爲的一部分mediaType或Header。所以路徑將保持不變。 – LaRRy

相關問題