我实际上正在开发一个小应用程序来训练,我在使用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;
}
我收到如下错误:
[com.sun.istack.internal.SAXException2]:无法封送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);
}
}
我做错了什么?
答案 0 :(得分:0)
您必须在DirectoryDTO中添加@XmlRootElement(name = "directoryEntity")
的注释。