编辑:这似乎也发生在 PUT 上。
使用 spring-data-rest-webmvc 版本 2.1.0.BUILD-SNAPSHOT 我发现我无法发布具有指向现有资源的关系的资源。我有 2 个这样的实体,它们需要实例化引用并发布到它们的任何一个端点都会导致以下行为。
在没有必需引用的情况下发布资源效果很好。
我做了一些挖掘,似乎PersistentEntityResourceHandlerMethodArgumentResolver
发现很好,但最终在检查是否可以反序列化类型MappingJackson2HttpMessageConverter
时抛出异常。ObjectMapper
异常的原因是NullPointerException
.
与 /reservations 有关系的示例 POST:
{
"description" : "test_post",
"dateMade" : "2014-03-03T08:04:44.293-0600",
"timeLastChanged" : null,
"userLastChanged" : null,
"courseTypeId" : null,
"numCredits" : null,
"instructor" : null,
"numParticipants" : null,
"reservationType" : "MCU",
"status" : "REQUESTED",
"abstract" : null,
"requestor" : "http://localhost:8080/users/2",
"submitter" : "http://localhost:8080/users/2",
"conferences" : []
}
回复:
{
cause: null
message: "No suitable HttpMessageConverter found to read request body into object of type class domain.Reservation from request with content type of application/json!"
}
POST 与 /roomGroups 没有关系:
{
"description" : "All Rooms",
"isOffNetwork" : false,
"roomGroupType" : "STANDARD"
}
回复:
201 Created
我发布的 JSON 是否有问题导致 NPE 来自ObjectMapper
?有某种解决方法吗?这在 2.0.0.RC1 中为我工作,使用稍微不同的方案在 JSON 中包含参考链接,并且由于杰克逊依赖项的版本似乎保持不变,我想知道是什么导致了这个问题......
谢谢你的帮助!
更新:
这个问题现在似乎与关联实体无关......
我创建了一个新的@Entity
ConnectionRequest 如下:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CONNECTION_REQUEST_ID")
private Long id;
@Column(name = "FROM_ENTITY_ID", nullable = false)
private Long fromId;
@Column(name = "TO_ENTITY_ID", nullable = false)
private Long toId;
@Convert(converter = EntityTypeConverter.class)
@Column(name = "FROM_ENTITY_TYPE_ID", nullable = false)
private EntityType fromType;
@Convert(converter = EntityTypeConverter.class)
@Column(name = "TO_ENTITY_TYPE_ID", nullable = false)
private EntityType toType;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
@JoinColumn(name = "CONFERENCE_ID", nullable = false)
private Conference conference;
我可以发布一条新的 ConnectionRequest 记录,其中包含 json 中包含的会议关系{"conference" : ".../conferences/1"}
。
@Entity
然而,我仍然得到与此预订相同的异常:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "RESERVATION_ID")
private Long id;
@Column(name = "DESCRIPTION", length = 50, nullable = false)
private String description;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DATE_MADE", nullable = false)
private Date dateMade;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TIME_LAST_CHANGED")
private Date timeLastChanged;
@Column(name = "USER_LAST_CHANGED")
private Integer userLastChanged; // TODO why is this an int?
@Column(name = "ABSTRACT", length = 2000)
private String _abstract;
@Column(name = "COURSE_TYPE_ID")
private Integer courseTypeId;
@Column(name = "NUMBER_OF_CREDITS")
private Integer numCredits;
@Column(name = "INSTRUCTOR", length = 255)
private String instructor;
@Column(name = "NUMBER_OF_PARTICIPANTS")
private Integer numParticipants;
@Convert(converter = ReservationTypeConverter.class)
@Column(name = "RESERVATION_TYPE_ID", nullable = false)
private ReservationType reservationType;
@Convert(converter = StatusConverter.class)
@Column(name = "STATUS_ID")
private Status status;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
@JoinColumn(name="REQUESTOR_USER_ID", nullable=false)
private User requestor;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
@JoinColumn(name="SUBMITTER_USER_ID", nullable=false)
private User submitter;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "reservation", cascade = {CascadeType.REMOVE})
private Set<Conference> conferences = new HashSet<>();
我不确定这门课有什么特别之处导致事情出错......