将资源传递给实体构造函数

时间:2016-11-17 12:16:02

标签: spring-data-rest

我有两个班级

@Entity
@Table(name="`order`")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne
    public Trader buyer;

    @ManyToOne
    public Trader seller;

    @ManyToOne
    public PriceTable priceTable;

    @OneToMany(cascade = CascadeType.ALL)
    public List<OrderItem> items = new ArrayList<>();

}

@Entity
public class OrderItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    public Double price;
    public Integer quantity;

    public String name;
    public String description;
    public String code;

    @ManyToOne
    public Item item;

    @ManyToMany
    public Set<Option> options;

    @JsonCreator(mode=JsonCreator.Mode.PROPERTIES)
    public OrderItem(
            @JsonProperty("quantity") Integer quantity,
            @JsonProperty("price") Double price,
            @JsonProperty("item") Item item

            ){
        this.quantity = quantity;
        this.price = price;
        this.item = item;
        this.name = item.name;

    }

}

OrderItemOrderRepository管理,并且没有自己的存储库。我将以下JSON发布为新订单:

{ 
   "seller":"http://localhost:8080/ErPApI/traders/1",
   "buyer":"http://localhost:8080/ErPApI/traders/2",
   "priceTable":"http://localhost:8080/ErPApI/priceTables/1",
   "items":[  
      {  
         "quantity":"5",
         "price":"13",
         "item":"http://localhost:8080/ErPApI/items/1"
      }
   ]
}

我得到了

  

“无法构造erp.model.oms.item.Item的实例:不   从String反序列化的String-argument构造函数/工厂方法   价值('http://localhost:8080/ErPApI/items/1')\ n在[来源:   org.apache.catalina.connector.CoyoteInputStream@676c47fe;行:9,   专栏:14](通过参考链:   erp.model.oms.order.Order [\“items \”] - &gt; java.util.ArrayList [0] - &gt; erp.model.oms.order.OrderItem [\“item \”])“

作为回应。我怎么能告诉spring“http://localhost:8080/ErPApI/items/1”是资源链接而不是字符串?

0 个答案:

没有答案