在我的Spring Data REST应用程序中使用Lombok来定义复杂类型时:
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "BOOK")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;
private String title;
@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
private Person author;
// ...
}
使用Spring Data REST控制器如下:
@RepositoryRestController
public class BookRepositoryRestController {
private final BookRepository repository;
@Autowired
public BookRepositoryRestController(BookRepository repository) {
this.repository = repository;
}
@RequestMapping(method = RequestMethod.POST,value = "/books")
public @ResponseBody PersistentEntityResource post(
@RequestBody Book book,
PersistentEntityResourceAssembler assembler) {
Book entity = processPost(book);
return assembler.toResource(entity);
}
private Book processPost(Book book) {
// ...
return this.repository.save(book);
}
}
我遇到了一个丑陋的错误:
no String-argument constructor/factory method to deserialize from String value
Spring Data REST使用Jackson处理Book POST请求的示例:
curl -X POST
-H 'content-type: application/json'
-d '{"title":"Skip Like a Pro", "author": "/people/123"}'
http://localhost:8080/api/books/
当Jackson尝试解析应该解析为单个唯一的Person
的/people/123
本地URI时,会发生反序列化错误。如果我删除@RepositoryRestController
,一切都正常。您对我的REST控制器定义有什么想法吗?
@AllArgsConstructor(suppressConstructorProperties = true)
,Jackson的反序列化会触发上述错误--这很可能是Lombok的一个bug。 - Jan Nielsen