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

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

问题描述:

在我的项目中,我有两个实体。调查和参与调查。他们是一对多的关系(对于一项调查,这可能是许多条目)。 Spring Data Rest&Lombok - 添加添加关系时的异常

@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数据休息2个休息库:

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

我已经成功地通过休息POST请求加入调查,我可以访问它条目(目前为空)发送GET到/api/surveys/1/entries。现在我想添加进入现有调查。虽然我可以通过发送POST(下面的内容)将其添加到/api/survey-entries我有麻烦直接添加它作为参考调查。我使用的POST方法具有相同的内容和URL /api/surveys/1/entries。有趣的是,我在日志中得到了NullPointerException,并且没有插入条目,但是更改了调查中的审计修改时间戳。我究竟做错了什么?我错过了相同的配置吗?或者我应该使用不同的内容? 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 

@EDIT

POST与调查

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

内容0

我试图通过添加与POST '应用/ 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

新增Lombok的注释本ö n实体类别

+0

显示您的每个请求的有效载荷,plz – Cepr0

+0

我已经添加了POST的主体与调查,但它工作正常,所以我不认为这与我的问题有关。 – Dcortez

+1

看到我的答案在其他主题:https://stackoverflow.com/a/46025434 – Cepr0

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

我设法通过将Lombok降级到版本(1.16.14)并将注释@AllArgsConstructor更改为@AllArgsConstructor(suppressConstructorProperties = true)来解决此问题。由于该属性目前已被删除,因此在后来的Lombok版本中无法实现。

我在Spring Data Rest JIRA上找到了解决方案。已经有问题DATAREST-884提及问题并提出解决方案/解决方法。

对不起,浪费时间,而无法看到没有所有代码的解决方案。

+0

您是否看到此信息? https://projectlombok.org/features/configuration'lombok.anyConstructor.suppressConstructorProperties如果为true,则在生成构造函数时,lombok不会生成@ java.beans.ConstructorProperties注释。这对于GWT和Android开发来说特别有用。' – Cepr0

+0

是的,但我没有将它与我的用例连接起来。我似乎认为这种注释使某些实体无法从uri反序列化。但为什么发生这对我来说是个谜。现在,当这个参数现在从龙目岛移除时,我认为我的用例是一个应该以某种方式解决的问题,而不需要降级依赖关系。 – Dcortez