2016-02-18 45 views
1

是否可以控制在Spring Data REST中返回搜索結果時使用的類名稱?Spring數據REST查詢中返回類型的控件名稱

我有一個類Account它是作爲JSON模式發佈的,並且不包含id字段,因爲這在RESTful API中應該是不透明的。爲了堅持使用Spring Data MongoDB,我擴展了AccountPersistableAccount,它有一個id字段。

當返回的搜索結果到客戶端,名稱persistableAccounts暴露,這是一個實現細節,不應該泄露到API:

{ 
    "_embedded" : { 
    "persistableAccounts" : [ { 
     "lastName" : "McLastName", 
     "firstName" : "Kevin", 
     "phoneNumber " : "+44 7700000000", 
     "email" : "[email protected]", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:64712/accounts/id" 
     }, 
     "persistableAccount" : { 
      "href" : "http://localhost:64712/accounts/id" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:64712/accounts/search/findByFirstName?firstName=Kevin" 
    } 
    } 
} 

是否有可能控制所使用的術語?

回答

0

如果您的結果可能由多個子類型組成,則Spring Data Rest始終包含一個collectionRel

默認情況下,它是類名,但是,您可以在存儲庫的@RepositoryRestResource註釋中對其進行自定義。

例如:

@RepositoryRestResource(collectionResourceRel = "whateverIwantHere") 
public interface CustomerRepository extends CrudRepository<Customer, Long> { 

} 
+0

謝謝!我想它會取自存儲庫的名稱,而不是其中一種通用類型。 –