2017-06-16 66 views
2

這是我的第二個問題在幾天內處理春季數據休息和HATEOAS鏈接。春天的數據休息HATEOAS鏈接和HTML選擇下拉

TL;博士

如何將我的應用程序中使用了鏈接,「多對一」屬性來填充「當前值」爲一個HTML選擇下拉?

「春天的數據休息」生成的鏈接行爲與「老學校ID」不同。

語境

  • 轉換某個應用彈簧JPA和彈簧數據其餘部分。按照以下
  • 我添加了一個投影名 '全' 到
  • 簡單 '的產品/類別' 表結構 '加入/獲取' 描述

*Product* 
--------- 
Product ID 
Product Name 
Category ID 

*Category* 
--------- 
Category ID 
Category Name 

彈簧數據其餘域對象

產品

@Entity 
@Table(name="products") 
public class Products 
{ 

    private static final long serialVersionUID = 5697367593400296932L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 

    public long id; 

    public String product_name; 

    @ManyToOne(optional = false,cascade= CascadeType.MERGE) 
    @JoinColumn(name = "category_id") 
    private ProductCategory productCategory; 

    public Products(){} 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public Products(String product_name) { 
     this.product_name = product_name; 
    } 

    public String getProduct_name() { 
     return product_name; 
    } 

    public void setProduct_name(String product_name) { 
     this.product_name = product_name; 
    } 

    public ProductCategory getProductCategory() 
    { 
     return productCategory; 
    } 

    public void setProductCategory(ProductCategory pProductCategory) 
    { 
     productCategory = pProductCategory; 
    } 


    @Projection(name = "full", types = { Products.class }) 
    interface VirtualProjection 
    { 

     String getProductName() ; 

     @Value("#{target.productCategory.category}") 
     String getCategory(); 

    } 

} 

產品類別

@Entity 
@Table(name="productcat") 

public class ProductCategory implements Serializable{ 

     private static final long serialVersionUID = 890485159724195243L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public long id; 
    public String category; 

    @OneToMany(mappedBy = "productCategory", cascade = CascadeType.ALL) 
    @JsonBackReference 
    Set<Products> products; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getCategory() { 
     return category; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public Set<Products> getProducts() { 
     return products; 
    } 

} 

問題:在對象

當角客戶端檢索一個產品類別的列表,值之間的阻抗不匹配,該產品的類別鏈接'的產品類別如下所示:

href" : "http://localhost:8080/api/rest/products/8/productCategory" 

當客戶端獲取的「產品類別」列表,鏈接的類別如下:

href": "http://localhost:8080/api/rest/productcat/4" 

這裏的「選擇」的代碼。如果「product.productCategory」爲「選項」設置了一個值,UI將僅顯示選擇。

<select ng-model="product.productCategory > 
    <option value="http://localhost:8080/api/rest/productcat/1">Foo Product Type 
    </option> 
    <option value="http://localhost:8080/api/rest/productcat/2">Bar Product Type 
    </option> 
    <option value="http://localhost:8080/api/rest/productcat/3">Baz Product Type 
    </option> 
</select> 

問題

  • 如何編寫我的服務器API,使 「生成的鏈接」 客戶 友好嗎?什麼是最靈活的方法?

  • 我是否應該更改API以接收更新的「值」而不是「ID/Links」,即類別名稱,而不是ID? (即並按名稱查找類別並在服務器端進行更新)

  • 您推薦使用哪些javascript/angular工具和技術來處理鏈接?

回答

0

我編寫了解決此問題的 '去參照' 通過JavaScript的 「產品分類」 值, - 檢索URL中的 「產品分類」 屬性,如:

http://localhost:8080/api/rest/products/8/productCategory 
  • 更換使用來自HATEOAS鏈接的值的「productCategory」屬性/ url。 pItem [pPropName] = data._links.self.href;

這種方法給簡單的下拉帶來了很多複雜性。它沒有正確的味道。

去參考代碼

function dereferenceProperty(pItem,pPropName) 
{ 

    var myRef = pItem._links[pPropName]; 
    if (myRef && myRef.href) 
    { 
     var myUrl = myRef.href; 
     $http.get(myUrl).success(function (data, status) 
     { 
      pItem[pPropName] = data._links.self.href; 
     }); 
    } 
} 

使用

dereferenceProperty(pProduct,"productCategory"); 

這種 '去refrence' 後:

  • 角/瀏覽器填充下拉正確
  • 一個HTTP PATCH更新E中的API(並表)正確
0

我不完全理解你是什麼意思,但如果你需要內ProductCategory得到Product列表,或者你需要得到隱含ProductCategoryProduct就可以使用,例如,projections

@Projection(name = "withProducts", types = ProductCategory.class) 
public interface WithProducts { 

    String getName(); 
    Set<Product> getProducts(); 
} 

@Projection(name = "withCategory", types = Product.class) 
public interface WithCategory { 

    String getName(); 
    BigDecimal getPrice(); 
    ProductCategory getCategory(); 
} 

然後如果你得到http://localhost:8080/api/productCategories/1?projection=withProducts您檢索是這樣的:

{ 
    "_embedded": { 
     "products": [ 
      { 
       "name": "Product1", 
       "category": { 
        "name": "Category1" 
       }, 
       "price": 1, 
       "_links": { 
        "self": { 
         "href": "http://localhost:8080/api/products/1" 
        }, 
        "product": { 
         "href": "http://localhost:8080/api/products/1{?projection}", 
         "templated": true 
        }, 
        "category": { 
         "href": "http://localhost:8080/api/products/1/category" 
        } 
       } 
      }, 
      { 
       "name": "Product2", 
       "category": { 
        "name": "Category1" 
       }, 
       "price": 2, 
       "_links": { 
        "self": { 
         "href": "http://localhost:8080/api/products/2" 
        }, 
        "product": { 
         "href": "http://localhost:8080/api/products/2{?projection}", 
         "templated": true 
        }, 
        "category": { 
         "href": "http://localhost:8080/api/products/2/category" 
        } 
       } 
      } 
     ] 
    }, 
    "_links": { 
     "self": { 
      "href": "http://localhost:8080/api/productCategories/1/products?projection=withCategory" 
     } 
    } 
} 

或者如果你http://localhost:8080/api/products/1/category?projection=withProducts您檢索是這樣的:

{ 
    "name": "Category1", 
    "products": [ 
     { 
      "name": "Product1", 
      "price": 1 
     }, 
     { 
      "name": "Product2", 
      "price": 2 
     } 
    ], 
    "_links": { 
     "self": { 
      "href": "http://localhost:8080/api/productCategories/1" 
     }, 
     "productCategory": { 
      "href": "http://localhost:8080/api/productCategories/1{?projection}", 
      "templated": true 
     }, 
     "products": { 
      "href": "http://localhost:8080/api/productCategories/1/products" 
     } 
    } 
} 

工作example