2015-06-23 67 views
0

在典型情況下與embedded resourcesSpring HATEOAS。嵌入資源+ CRUD的能力呢?

@Entity @Data 
class Item { 
    id, name 
    ... 
    @ManyToOne 
    @JoinColumn(name="status", referencedColumnName="ID") 
    private Status status; 
} 

@Entity @Data 
class Status { 
    id, name 
    ... 
    @JsonIgnore //break infinite reference loop during serialization 
    @OneToMany(mappedBy="status") 
    private List<Item> items; 
} 

而不必鏈接狀態ID的項JSON的,我想包括狀態對象項目 JSON

 { 
      "itemName": "abc", 
       ... , 
      "status": { 
       "statusName":"ACTIVE", 
        ... 
      } 
      "_links": { 
       ... 
      } 
     } 

我設法嵌入做任何以下:

  • 標記項目狀態財產@RestResource(出口= FALSE)
@Entity @Data 
    class Item { 
     ... 
     @RestResource(exported=false) // <-- HERE 
     @ManyToOne 
     @JoinColumn(name="status", referencedColumnName="ID") 
     private Status status; 
  • 標誌狀態回購接口@RepositoryRestResource(...,出口= FALSE)
@RepositoryRestResource(collectionResourceRel="statuses", path="status", exported=false) 
    public interface StatusRepository extends JpaRepository<Status, String> 
  • Deleteting的狀態實體庫
// DELETED 
    @RepositoryRestResource 
    public interface StatusRepository extends JpaRepository<Status, String>{} 

問題:

任何的嵌入狀態到項目JSON就像我想要的,但我沒有狀態儲存庫的訪問了通過它的ID獲取一個Status對象,或者對它做任何CRUD。

如何在父項中嵌入狀態項目JSON和通過url仍CRUD狀態?

回答

0

我實現它,如下所示:

@Entity @Data 
class Item { 
    @Id 
    @GeneratedValue(generator = "uuid2") 
    @GenericGenerator(name = "uuid2", strategy = "uuid2") 
    String id; 
    String name 

    @RestResource(exported = false) // <---- ADDED 
    @ManyToOne 
    @JoinColumn(name="status", referencedColumnName="ID") 
    private Status status; 
} 

@Entity @Data 
class Status { 
    @Id 
    @GeneratedValue(generator = "uuid2") 
    @GenericGenerator(name = "uuid2", strategy = "uuid2") 
    String id 
    String name 

    // <-------- DELETED REFERENCE TO PARENT OBJECT 
    //@OneToMany(mappedBy="status") 
    //private List<Item> items; 
} 

庫爲兩個實體存在,因此它是可以做到CRUD上的狀態的實體以及

@RepositoryRestResource(collectionResourceRel="items", path="items") 
public interface StatusRepository extends JpaRepository<Item, String> 
{} 

@RepositoryRestResource(collectionResourceRel="statuses", path="statuses") 
public interface StatusRepository extends JpaRepository<Status, String> 
{} 

爲了子孫後代我會把它留在這裏。

爲了方便:

1)實體暴露在IDS JSON

/** 
* Exposing Ids as properties for entities specified 
*/ 
@Configuration 
public class ExposeIdConfig extends RepositoryRestMvcConfiguration { 
    @Override 
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
    config.exposeIdsFor(Item.class, Status.class); 
    } 
} 

因此獲得的產品清單的樣子:

{ 
    "_links": { 
    "self": { 
     "href": "http://localhost:8080/app/items{?page,size,sort}", 
     "templated": true 
    } 
}, 
"_embedded": { 
    "as": [ 
    { 
     "id": "29117425-f011-4ff9-8952-38b05d3df7f0", 
     "name": "item 1", 
     "status": { 
     "id": "e9192ae7-29f8-4d5e-ad62-cad8d87de9e2", 
     "name": "ACTIVE" 
     }, 
     "_links": { 
     "self": { 
      "href": "http://localhost:8080/app/items/29117425-f011-4ff9-8952-38b05d3df7f0" 
     } 
    }, 
    ... 
}