Spring Data Rest 2.1.0无法POST或PUT复杂资源 [英] Spring Data Rest 2.1.0 Cannot POST or PUT Complex Resource

查看:120
本文介绍了Spring Data Rest 2.1.0无法POST或PUT复杂资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这似乎也发生在PUT上。

This appears to be happening with PUTs as well.

使用spring-data-rest-webmvc版本2.1.0.BUILD-SNAPSHOT我发现我无法使用指向已存在资源的关系来发布资源。我有2个这样的实体,它们需要实例化引用,并且POST到它们的任何一个端点都会产生下面的行为。

Using spring-data-rest-webmvc version 2.1.0.BUILD-SNAPSHOT I have found that I am unable to POST a resource with a relation pointing to an already existing resource. I have 2 such entities which require references to be instantiated and POSTing to either of their endpoints results in the behavior below.

在没有必要引用的情况下发布资源效果很好。

POSTing a resource without required references works well.

我做了一些挖掘,似乎 PersistentEntityResourceHandlerMethodArgumentResolver 找到 MappingJackson2HttpMessageConverter 很好,但最终会在检查 ObjectMapper 是否可以反序列化类型时抛出异常。异常的原因是 NullPointerException

I did a bit of digging and it appears that PersistentEntityResourceHandlerMethodArgumentResolver finds the MappingJackson2HttpMessageConverter just fine, but it ends up throwing an exception while checking whether the ObjectMapper can deserialize the type. The cause of the exception is a NullPointerException.

示例POST与/预订的关系:

example POST w/ relations to /reservations:

{
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,
conference:[]
}

回复:

{
cause:null
message:找不到合适的HttpMessageConverter来读取请求body进入类domain的类型的对象。从请求开始,内容类型为application / json!
}

POST与/ roomGroups没有任何关系:

POST w/ no relations to /roomGroups:

{
description:All Rooms,
isOffNetwork:false,
roomGroupType:STANDARD
}

回应:

201已创建

我是POST的JSON有什么问题,导致 ObjectMapper 中的NPE?有某种解决方法吗?这在2.0.0.RC1中对我有用,使用稍微不同的方案在JSON中包含引用链接,因为Jackson依赖项的版本看起来保持不变我不知道是什么导致了这个问题......

Is there something wrong about the JSON I am POSTing which is resulting in an NPE from the ObjectMapper? Is there a workaround of some kind? This was working for me in 2.0.0.RC1 using a slightly different scheme for including reference links in the JSON and since the version of the Jackson dependencies appears to have stayed the same I wonder what is causing this issue...

感谢您的帮助!

更新:

这现在问题似乎与相关实体无关......

This issue now seems un-related to the associated entities...

我创建了一个新的 @Entity ConnectionRequest如下:

I created a new @Entity ConnectionRequest as follows:

@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:... / meetings / 1}

I can POST a new ConnectionRequest record with a Conference relation included in the json as such {"conference" : ".../conferences/1"}.

然而,我仍然得到相同的异常w /这 @Entity 预订:

I am however still getting the same exception w/ this @Entity Reservation:

@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<>();

我不确定这个课程有什么特别之处导致事情出错......

I'm not sure what's special about this class that's causing things to go awry...

推荐答案

问题如下:

这两个都是非发布的实体有一个名为 _abstract 的属性,因为它是Java中的保留字。我已经为此属性命名了getter和setter getAbstract() setAbstract()

Both of the non-postable entities had a property called _abstract due to it being a reserved word in Java. I had named the getter and setter for this property getAbstract() and setAbstract() respectively.

杰克逊似乎一直在抛出空指针异常,因为getter和setter与预期的属性名称不匹配。

Jackson appears to have been throwing a null pointer exception since the getter and setter did not match the property name as expected.

我将属性名称更改为 resvAbstract 并将访问者更新为 getResvAbstract() setResvAbstract( )所有东西都聚集在一起开始工作。

When I changed the property name to resvAbstract and updated the accessors to getResvAbstract() and setResvAbstract() everything came together and started working.

我仍然对导致此问题出现的变化感到好奇,但我是很高兴它正在工作!

I'm still curious about the change that led to this issue showing up, but I'm glad it's working!

这篇关于Spring Data Rest 2.1.0无法POST或PUT复杂资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆