我实际上正在开发一个小应用程序来训练,我在使用
SpringHateoas使用PathVariable时遇到了问题.
事实上,当我使用类似的东西:
@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]: unable to marshal type DirectoryDTO
to element because it’s missing an annotation
这是我的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”)的注释.