Spring Data Rest & Lombok - 添加关系时出现异常
Spring Data Rest & Lombok - Exception while adding adding relation
在我的项目中,我有 2 个实体。调查和调查条目。它们是一对多的关系(一项调查可以有多个条目)。
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "survey_entries")
@TypeDef(name = "SurveyEntry", typeClass = SurveyEntry.class)
public class SurveyEntryEntity extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "survey_id")
private SurveyEntity survey;
@NonNull
@Type(type = "SurveyEntry")
@Column(name = "responses")
// JSON db column type mapped to custom type
private SurveyEntry responses;
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "surveys")
@TypeDef(name = "Survey", typeClass = Survey.class)
public class SurveyEntity extends AbstractEntity {
@NonNull
@Type(type = "Survey")
@Column(name = "template")
// JSON db column type mapped to custom type
private Survey survey;
@OneToMany(mappedBy = "survey")
private List<SurveyEntryEntity> entries;
}
我还使用 Spring Data Rest 创建了 2 个 rest 存储库:
@RepositoryRestResource(collectionResourceRel = "survey_entries", path = "survey-entries")
public interface SurveyEntryRepository extends PagingAndSortingRepository<SurveyEntryEntity, Long> {
}
@RepositoryRestResource(collectionResourceRel = "surveys", path = "surveys")
public interface SurveyRepository extends PagingAndSortingRepository<SurveyEntity,Long> {
}
我已经通过 rest POST 请求成功添加了调查,我可以通过将 GET 发送到 /api/surveys/1/entries
来访问它的条目(当前为空)。现在我想将条目添加到现有调查中。虽然我可以通过将 POST(以下内容)发送到 /api/survey-entries
来添加它,但我无法直接将其添加为调查参考。我正在使用具有相同内容和 url /api/surveys/1/entries
的 POST 方法。有趣的是,我在日志中收到 NullPointerException,并且未插入条目,但调查中的审核修改时间戳已更改。我究竟做错了什么?我错过了相同的配置吗?或者我应该使用不同的内容?
POST 的内容,条目:
{
"responses": {
"question1": "response1",
"question2": "response2",
"question3": "response3"
}
}
POST 的调查内容:
{
"survey": {
//survey structure
}
}
异常:
08:41:14.730 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Failed to resolve argument 1 of type 'org.springframework.data.rest.webmvc.PersistentEntityResource'
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: No content to map due to end-of-input; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
@编辑
我尝试将 POST 的条目添加到 /api/survey-entries
和 'application/hal+json' Content-Type header 以及如下内容,但现在我得到了其他异常:
内容:
{
"survey" : "http://localhost:8080/api/surveys/1",
"responses": {
"question1": "response1",
"question2": "response2",
"question3": "response3"
}
}
异常:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.domain.SurveyEntity` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/surveys/1')
at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 41] (through reference chain: com.domain.SurveyEntryEntity["survey"])
@编辑 2
在实体类上添加了 Lombok 注释
不幸的是,问题出在示例代码中未包含的 Lombok 注释中。我现在添加了它们,这样任何人都可以看到问题出在哪里。
我设法通过将 Lombok 降级到版本 (1.16.14) 并将注释 @AllArgsConstructor
更改为 @AllArgsConstructor(suppressConstructorProperties = true)
来解决它。在以后的 Lombok 版本中不可能实现,因为这个 属性 目前已被删除。
我在 Spring Data Rest JIRA 上找到了解决方案。已经有问题 DATAREST-884 提到问题并提出 solution/workaround。
很抱歉浪费了时间,没有所有代码就不可能看到解决方案。
在我的项目中,我有 2 个实体。调查和调查条目。它们是一对多的关系(一项调查可以有多个条目)。
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "survey_entries")
@TypeDef(name = "SurveyEntry", typeClass = SurveyEntry.class)
public class SurveyEntryEntity extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "survey_id")
private SurveyEntity survey;
@NonNull
@Type(type = "SurveyEntry")
@Column(name = "responses")
// JSON db column type mapped to custom type
private SurveyEntry responses;
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "surveys")
@TypeDef(name = "Survey", typeClass = Survey.class)
public class SurveyEntity extends AbstractEntity {
@NonNull
@Type(type = "Survey")
@Column(name = "template")
// JSON db column type mapped to custom type
private Survey survey;
@OneToMany(mappedBy = "survey")
private List<SurveyEntryEntity> entries;
}
我还使用 Spring Data Rest 创建了 2 个 rest 存储库:
@RepositoryRestResource(collectionResourceRel = "survey_entries", path = "survey-entries")
public interface SurveyEntryRepository extends PagingAndSortingRepository<SurveyEntryEntity, Long> {
}
@RepositoryRestResource(collectionResourceRel = "surveys", path = "surveys")
public interface SurveyRepository extends PagingAndSortingRepository<SurveyEntity,Long> {
}
我已经通过 rest POST 请求成功添加了调查,我可以通过将 GET 发送到 /api/surveys/1/entries
来访问它的条目(当前为空)。现在我想将条目添加到现有调查中。虽然我可以通过将 POST(以下内容)发送到 /api/survey-entries
来添加它,但我无法直接将其添加为调查参考。我正在使用具有相同内容和 url /api/surveys/1/entries
的 POST 方法。有趣的是,我在日志中收到 NullPointerException,并且未插入条目,但调查中的审核修改时间戳已更改。我究竟做错了什么?我错过了相同的配置吗?或者我应该使用不同的内容?
POST 的内容,条目:
{
"responses": {
"question1": "response1",
"question2": "response2",
"question3": "response3"
}
}
POST 的调查内容:
{
"survey": {
//survey structure
}
}
异常:
08:41:14.730 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Failed to resolve argument 1 of type 'org.springframework.data.rest.webmvc.PersistentEntityResource'
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: No content to map due to end-of-input; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
@编辑
我尝试将 POST 的条目添加到 /api/survey-entries
和 'application/hal+json' Content-Type header 以及如下内容,但现在我得到了其他异常:
内容:
{
"survey" : "http://localhost:8080/api/surveys/1",
"responses": {
"question1": "response1",
"question2": "response2",
"question3": "response3"
}
}
异常:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.domain.SurveyEntity` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/api/surveys/1')
at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 1, column: 41] (through reference chain: com.domain.SurveyEntryEntity["survey"])
@编辑 2
在实体类上添加了 Lombok 注释
不幸的是,问题出在示例代码中未包含的 Lombok 注释中。我现在添加了它们,这样任何人都可以看到问题出在哪里。
我设法通过将 Lombok 降级到版本 (1.16.14) 并将注释 @AllArgsConstructor
更改为 @AllArgsConstructor(suppressConstructorProperties = true)
来解决它。在以后的 Lombok 版本中不可能实现,因为这个 属性 目前已被删除。
我在 Spring Data Rest JIRA 上找到了解决方案。已经有问题 DATAREST-884 提到问题并提出 solution/workaround。
很抱歉浪费了时间,没有所有代码就不可能看到解决方案。