Spring Data Rest & Lombok - 添加关系时的异常



在我的项目中,我有2个实体。调查和调查条目。它们是一对多(Thary可以是一项调查的许多条目)。

@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;
}

我还使用春季数据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请求添加了调查,可以通过发送到/api/surveys/1/entries来访问该条目(当前为空)。尽管我可以通过将帖子(以下内容)发送到/api/survey-entries来添加它,但我遇到了直接添加它作为调查的引用。我正在使用具有相同内容和URL /api/surveys/1/entries的Post方法。有趣的是,我在日志中获得了nullpoInterException,并且没有插入输入,但是审核修改了调查中的时间戳。我究竟做错了什么?我错过了同样的配置吗?还是我应该使用不同的内容?

帖子内容与条目:

{      
  "responses": {          
    "question1": "response1",          
    "question2": "response2",          
    "question3": "response3"      
  }  
}

调查的帖子内容:

{
    "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

@edit

我尝试使用'application/hal json'content-type标题和内容的邮政邮寄添加到 /api/survey-entries,但现在我得到了其他例外:

内容:

{      
  "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"])

@Edit 2

添加了实体classess上存在的LOMBOK注释

不幸的问题在于LOMBOK注释中未包含在示例代码中。我现在添加了它们,以便任何人都可以看到问题所在。

我通过将Lombok降级到版本(1.16.14)并将注释@AllArgsConstructor降低到@AllArgsConstructor(suppressConstructorProperties = true)来解决。由于目前已删除此属性,因此在以后的Lombok版本中实现是不可思议的。

我已经在弹簧数据REST JIRA上找到了解决方案。已经存在问题datarest-884提及问题并提出解决方案/解决方法。

很抱歉浪费时间,而没有所有代码就无法看到解决方案。

相关内容

最新更新