【发布时间】:2020-06-16 09:35:37
【问题描述】:
我有一个如下所示的 Job 类:
@Entity
@Table
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Data
@NoArgsConstructor
public class Job implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(nullable = false)
private String job;
@ManyToOne
@JsonIgnoreProperties("jobs")
private Job next;
以及创造工作的方法:
@PostMapping("/jobs")
public ResponseEntity<Job> createJob(@Valid @RequestBody Job job) throws URISyntaxException {
Job result = jobRepository.save(job);
return ...
}
直到这里一切正常。现在将 @AllArgsConstructor 添加到 Job 类会导致 MismatchedInputException。
Bad Request: JSON parse error: Cannot construct instance of `xxx.domain.Job` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `xxx.domain.Job` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
我尝试将 lombok.anyConstructor.addConstructorProperties = true 添加到 lombok.config 文件中,如我找到的可能答案中所述。但这只会导致以下错误:
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ project-name ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 27 source files to /path/target/test-classes
Unknown key 'lombok.anyConstructor.addConstructorProperties' (/path/lombok.config:2)
我尝试使用旧版本的 lombok,因为我在更新 lombok 时发现了一些解决此问题的 github 问题。另外,没有成功(试过1.16.18版)
我还尝试删除private Job next 属性,该属性使代码正常工作而没有任何错误。所以这一定是个问题?
【问题讨论】:
标签: java json spring jackson lombok