编辑:这似乎也发生PUTs以及。春季数据休息2.1.0不能POST或PUT复杂资源
使用spring-data-rest-webmvc版本2.1.0.BUILD-SNAPSHOT我发现我无法使用指向已有资源的关系发布资源。我有2个这样的实体需要参考实例化,并发布到他们的任何一个端点导致下面的行为。
在不需要引用的情况下发布资源效果很好。
我做了一些挖掘,看起来PersistentEntityResourceHandlerMethodArgumentResolver
发现MappingJackson2HttpMessageConverter
很好,但它最终在检查ObjectMapper
是否可以反序列化类型时抛出异常。异常的原因是NullPointerException
。
例如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" : [] }
RESPONSE:
{ 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我张贴这是导致从ObjectMapper
的NPE?有什么解决方法吗?这在2.0.0.RC1中为我工作,使用稍微不同的方案来包含JSON中的引用链接,并且由于杰克逊依赖关系的版本似乎保持不变,所以我不知道是什么导致此问题...
感谢您的帮助!
UPDATE:
这个问题现在看来未相关的关联实体...
我创建了一个新的@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中的会议关系的新ConnectionRequest记录,如{"conference" : ".../conferences/1"}
。
不过,我仍然得到同样的异常W /本@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<>();
我不知道有什么特别之处这个类是造成事情出差错......
您是否有机会添加领域模型的相关部分?随意浏览[测试用例| https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-webmvc/src/test/java/org/springframework/ data/rest/webmvc/jpa/DataRest262Tests.java#L85]添加到DATAREST-262并查看它与您的设置有何不同。 –