【问题标题】:Spring-data-rest POST link throws Null-Pointer ExceptionSpring-data-rest POST 链接抛出空指针异常
【发布时间】:2014-07-31 04:00:44
【问题描述】:

我有一个名为 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)

我做错了什么?

【问题讨论】:

  • 不应该是"version": "http://localhost:8111/storefront/rest/version/3378719354003680"吗?
  • 你的 VersionModel 实体是什么样子的?
  • 这是版本模型:
  • @Cacheable @Entity(name = VersionModel.NAME) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Table(name = VersionModel.NAME, uniqueConstraints = { @UniqueConstraint(columnNames = { "pk" } ) }, 索引 = { @Index(columnList = "id")}) 公共类 VersionModel 扩展 AbstractEntityModel { public static final String NAME = "version"; }
  • 这个网站太垃圾了!!!!!!我无法在回复中发布代码 - 我添加了 4 个空格,但它仍然没有显示为代码。

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


【解决方案1】:

如果您想在创建资源时直接创建与相关资源的关联,则为关系提交的值必须直接是 URI,而不是某种对象。如果您像这样调整它,您展示的示例将起作用:

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

【讨论】:

  • 它起作用了(一些字符使它超过 8 个,这样这个愚蠢的网站就会接受我的评论)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 2020-11-22
  • 2020-08-10
  • 2013-03-21
  • 1970-01-01
相关资源
最近更新 更多