2017-10-04 27 views
0

通過公開的MongoDB的集合與SpringBoot REST服務,我的服務做一個HTTP GET時取回這樣的文件MongoDB的公開爲REST與SpringBoot:什麼是返回的對象類型

$ curl http://localhost:8080/people 
{ 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8080/people{?page,size,sort}", 
     "templated" : true 
    }, 
    "search" : { 
     "href" : "http://localhost:8080/people/search" 
    } 
    }, 
"_embedded" : { 
    "persons" : [ { 
     "firstName" : "Frodo", 
     "lastName" : "Baggins", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/people/53149b8e3004990b1af9f229" 
     } 
     } 
    } ] 
    }, 
    "page" : { 
    "size" : 20, 
    "totalElements" : 1, 
    "totalPages" : 1, 
    "number" : 0 
    } 
} 

集合中包含的人在「_embedded」部分。

我想只使用人員列表,那麼要使用的URL是什麼?或者,這個返回的文檔的對象類型是什麼,以便我可以反序列化它?

回答

0

這些僅僅是Spring Mongo數據靜止API中可用的休息端點。

{[/{people}],methods=[OPTIONS],produces=[application/hal+json || application/json]} 
{[/{people}],methods=[HEAD],produces=[application/hal+json || application/json]} 
{[/{people}],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}],methods=[GET],produces=[application/x-spring-data-compact+json || text/uri-list] 
{[/{people}],methods=[POST],produces=[application/hal+json || application/json]} 
{[/{people}/{id}],methods=[OPTIONS],produces=[application/hal+json || application/json]} 
{[/{people}/{id}],methods=[HEAD],produces=[application/hal+json || application/json]} 
{[/{people}/{id}],methods=[GET],produces=[application/hal+json || application/json]} 
{{/{people}/{id}],methods=[PUT],produces=[application/hal+json || application/json]} 
{[/{people}/{id}],methods=[PATCH],produces=[application/hal+json || application/json]} 
{[/{people}/{id}],methods=[DELETE],produces=[application/hal+json || application/json]} 
{[/ || ],methods=[OPTIONS],produces=[application/hal+json || application/json]} 
{[/ || ],methods=[HEAD],produces=[application/hal+json || application/json]} 
{[/ || ],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}/{id}/{property}],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}/{id}/{property}/{propertyId}],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}/{id}/{property}],methods=[DELETE],produces=[application/hal+json || application/json]} 
{[/{people}/{id}/{property}],methods=[GET],produces=[application/x-spring-data-compact+json || text/uri-list]} 
{[/{people}/{id}/{property}],methods=[PATCH || PUT || POST],consumes=[application/json || application/x-spring-data-compact+json || text/uri-list],produces=[application/hal+json || application/json]} 
{[/{people}/{id}/{property}/{propertyId}],methods=[DELETE],produces=[application/hal+json || application/json]} 
{[/{people}/search],methods=[HEAD],produces=[application/hal+json || application/json]} 
{[/{people}/search],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}/search],methods=[OPTIONS],produces=[application/hal+json || application/json]} 
{[/{people}/search/{search}],methods=[GET],produces=[application/hal+json || application/json]} 
{[/{people}/search/{search}],methods=[GET],produces=[application/x-spring-data-compact+json]} 
{[/{people}/search/{search}],methods=[OPTIONS],produces=[application/hal+json || application/json]} 
{[/{people}/search/{search}],methods=[HEAD],produces=[application/hal+json || application/json]} 

如果我們知道那個特定文檔的_id,我們可以得到一個人文檔。

我們只能使用搜索API獲取_embedded json對象。例如,

@RepositoryRestResource(collectionResourceRel = "people", path = "people") 
public interface PersonRepository extends MongoRepository<Person, String> { 

     List<Person> findAllByIdIsNotNull(); 

} 

由於_id不會在MongoDB中爲空,我們可以得到所有的文件,如果我們打這個端點http://localhost:8080/people/search/findAllByIdIsNotNull。答覆如下。

{ 
"_embedded": { 
    "people": [{ 
      "firstName": "N1", 
      "lastName": "N2", 
      "_links": { 
       "self": { 
        "href": "http://localhost:8080/people/59d54d2bb13148a7e93953c8" 
       }, 
       "person": { 
        "href": "http://localhost:8080/people/59d54d2bb13148a7e93953c8" 
       } 
      } 
     } 
} 

只得到個人文件的最好方法是通過使用一些JSON解析API,如jq在shell腳本或GSON /傑克遜在Java等

希望這有助於。

相關問題