2017-03-04 72 views
0

我有幾個端點類,我已經暴露給我的Android客戶端。這些端點中的方法採用實體類型參數。如您所知,端點已通過@Api註釋公開,我沒有使用@Api註釋實體類型參數。我面臨的問題是這些端點共享一組實體類,但是當我生成端點庫時,會爲相同類的每個端點生成這些實體類型。我想爲所有端點擁有這些類的通用集合。讓我給你舉個例子來闡述這個問題:如何在Google雲端點之間共享課程?

比方說,我有註釋的端點:

@Api(
     name = "learnerProfileVer1Api", 
     version = "v1", 
     resource = "learnerProfileVer1", 
     namespace = @ApiNamespace(
       ownerDomain = "create.account.backend.learncity.com", 
       ownerName = "create.account.backend.learncity.com", 
       packagePath = "" 
     ) 
) 
public class Endpoint1{ 

@ApiMethod(
      name = "insert", 
      path = "learnerProfileVer1", 
      httpMethod = ApiMethod.HttpMethod.POST) 
    public LearnerProfileVer1 insert(LearnerProfileVer1 learnerProfileVer1) {....} 

} 

而另一個端點,

@Api(
     name = "searchApi", 
     version = "v1", 
     title = "Search API", 
     namespace = @ApiNamespace(
       ownerDomain = "learncity.com", 
       ownerName = "Learncity", 
       packagePath = "" 
     ) 
) 
public class SearchTutorsEndpoint { 

@ApiMethod(name = "searchTutors", 
      httpMethod = ApiMethod.HttpMethod.POST) 
    public CollectionResponse<TutorProfileVer1> searchTutors(SearchTutorsQuery searchTutorsQuery, @Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {...} 

} 

你可以在上面看到2個端點,我有LearnerProfileVer1和​​實體類型參數。這兩個類使用了其他一些很常見的類。

現在,當我生成客戶庫,我得到1 learnerProfileVer1Api具有model包在它在那裏它具有的那些種類和我得到客戶-lib的分別爲SearchApi具有單獨model包具有相同的類。當我將我的客戶端代碼鏈接到這些客戶端庫時,這個問題會放大,我發現同一類的3個不同版本。

有沒有解決方案,使這些類有一套客戶端庫生成?

回答

0

這個問題沒有任何優雅的解決方案,但有一個我發現使用Multiclass API。我不得不重新設計我的後端代碼,這在客戶端打破了我的代碼,但它是值得的,並且在項目複雜性增加時意識到它。無論如何,你可以閱讀關於Multiclass API here

下面是一個簡單的例子,你可能會這樣做。

/** 
    * An endpoint class we are exposing 
    */ 
    @Api(
      name = "userApi", 
      version = "v1", 
      namespace = @ApiNamespace(
        ownerDomain = "backend.myapplication.DJ.example.com", 
        ownerName = "backend.myapplication.DJ.example.com", 
        packagePath = "" 
      ) 
    ) 
    @ApiClass(
      resource = "account" 
    ) 
    public class AccountEndpoint { 

     /** 
     * A simple endpoint method that takes a name and says Hi back 
     */ 
     @ApiMethod(name = "sayHi") 
     public MyBean sayHi(@Named("name") String name) { 
      MyBean response = new MyBean(); 
      response.setData("Hi, " + name); 

      return response; 
     } 

    } 


/** 
* Another endpoint class we are exposing 
*/ 
@Api(
     name = "userApi", 
     version = "v1", 
     namespace = @ApiNamespace(
       ownerDomain = "backend.myapplication.DJ.example.com", 
       ownerName = "backend.myapplication.DJ.example.com", 
       packagePath = "" 
     ) 
) 
@ApiClass(
     resource = "search" 
) 
public class SearchEndpoint { 

    private static final Logger logger = Logger.getLogger(SearchEndpoint.class.getName()); 

    /** 
    * This method gets the <code>MyBean</code> object associated with the specified <code>id</code>. 
    * 
    * @param id The id of the object to be returned. 
    * @return The <code>MyBean</code> associated with <code>id</code>. 
    */ 
    @ApiMethod(name = "getMyBean") 
    public MyBean getMyBean(@Named("id") Long id) { 
     // TODO: Implement this function 
     logger.info("Calling getMyBean method"); 
     return null; 
    } 

    /** 
    * This inserts a new <code>MyBean</code> object. 
    * 
    * @param myBean The object to be added. 
    * @return The object to be added. 
    */ 
    @ApiMethod(name = "insertMyBean") 
    public MyBean insertMyBean(MyBean myBean) { 
     // TODO: Implement this function 
     logger.info("Calling insertMyBean method"); 
     return myBean; 
    } 
} 

重要的是,你可以在這裏看到的是,2層的API通過資源在@APIClass註釋不同。

這2個端點接受賬戶相關請求和搜索用戶的相關請求。這是2個API之間的邏輯關係(分組在userApi下)。雖然它看起來並不壞,但你最終會在同一個軟件包中的所有端點方法以及API瀏覽器中找到並測試有點乏味的東西,但考慮到你將會涉及的設計混亂,這是完全值得的。當您添加另一個訪問至少一個現有端點使用的類的API時,此解決方案變得更加乏味。您將被迫將其包含在現有的API中。如果這個新的API只有一個或兩個共同的實體,那麼您可能會通過在同一個API中將它分組(在處理客戶端的特殊情況時處理這些重複的模型類)而在客戶端造成不顯着的設計危害

以下幾點值得多類使用API​​時,必須牢記:

  • 由於API身份是所有端點相同,方法名必須有不同的簽名。
  • 看到沒有兩種不同的方法最終具有相同的REST路徑。
  • 將資源,方法名稱,API方法名稱命名爲儘可能合理,以便在對新API進行分組時可以建立這些名稱。