Spring Data Rest 2.1.0无法POST或PUT复杂资源

时间:2014-03-03 16:08:58

标签: spring spring-mvc jackson spring-data spring-data-rest

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

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

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

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

示例POST /与/ 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", "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

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

感谢您的帮助!

更新:

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

我创建了一个新的@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;

我可以使用json中包含的会议关系POST一个新的ConnectionRequest记录{"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<>();

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

1 个答案:

答案 0 :(得分:0)

问题如下:

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

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

当我将属性名称更改为resvAbstract并将访问者更新为getResvAbstract()setResvAbstract()时,所有内容都汇集在一起​​并开始工作。

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