Spring-data-rest POST链接抛出Null-Pointer异常

时间:2014-06-10 11:46:19

标签: spring rest spring-data spring-data-jpa spring-data-rest

我有一个名为StockLevel的实体和另一个名为Version的实体,他们的关系是这样的:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "version", nullable = false, referencedColumnName = "pk")
private VersionModel version;

现在我想通过Spring-Data-REST创建一个新的StockLevel,所以我向http://somedomain.com:8111/storefront/rest/stock_level/发出以下POST请求

{
   "uid" : "mynewuidForStockLevel",
   "version" : 
   {
      "rel" : "version",
      "href":"http://localhost:8111/storefront/rest/version/3378719354003680"
   }
}

这是我的Version模型:

@Cacheable 
@Entity(name = VersionModel.NAME) 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@Table(name = VersionModel.NAME, 
       uniqueConstraints = { @UniqueConstraint(columnNames = { "pk" }) }, 
       indexes = { @Index(columnList = "id")}) 
public class VersionModel extends AbstractEntityModel {
    public static final String NAME = "version";
} 

但是我在Fasterxml Jackson中得到了一个空指针异常:

Caused by: java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3023)
at java.net.URI.<init>(URI.java:595)
at java.net.URI.create(URI.java:857)
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:359)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)

我做错了什么?

1 个答案:

答案 0 :(得分:3)

如果要在创建资源时直接创建与相关资源的关联,则为关系提交的值必须是直接的URI,而不是某些类型的对象。如果您按照以下方式调整,您展示的样本将起作用:

{ "uid" : "mynewuidForStockLevel",
  "version" : "http://localhost:8111/storefront/rest/version/3378719354003680" }