我实际上是在开发一些训练应用程序,并且在使用PathVariable的Spring Hateoas时遇到了问题。
实际上,当我使用类似的东西时:
@RequestMapping(value = "/directories/{idDirectory}", method = RequestMethod.GET)
public DirectoryDTO findById(@PathVariable String idDirectory) {
DirectoryEntity directoryEntity = directoryService.findById(idDirectory);
DirectoryDTO directoryDto = new DirectoryDTO(directoryEntity);
directoryDto.add(linkTo(methodOn(DirectoryController.class).findById(idDirectory)).withSelfRel());
return directoryDto;
}
我遇到类似以下错误:
这是我的DirectoryEntity:
@Document(collection = "directory")
public class DirectoryEntity {
@Id
private String id;
private String name;
private String path;
private List<DirectoryEntity> childrenDirectories;
private DirectoryEntity parentDirectory;
private List<FileEntity> fileEntities;
/* Get/set omitted */
}
和DTO:
public class DirectoryDTO extends Resource<DirectoryEntity> {
public DirectoryDTO(DirectoryEntity content, Link... links) {
super(content, links);
}
public DirectoryDTO(DirectoryEntity content, Iterable<Link> links) {
super(content, links);
}
}
我究竟做错了什么 ?
最佳答案
您必须在DirectoryDTO中添加@XmlRootElement(name = "directoryEntity")
的注释。