【发布时间】:2016-05-30 09:51:21
【问题描述】:
我正在使用 Spring Data Rest 创建一个 RESTful api。我想处理返回实体表示的异常,例如由 Spring Data Rest 存储库(带有 HATEOAS 链接)生成的实体表示。我需要返回实体表示的方法如下:
@ExceptionHandler(value = {ExistentUGVException.class})
@ResponseBody
protected ResponseEntity<UGV> existentUGVHandler(HttpServletRequest request, HttpServletResponse response, ExistentUGVException ex) {
return new ResponseEntity<UGV>(ex.ugv, HttpStatus.OK);
}
此实现返回不带链接的 UGV 表示:
{
"title" : "Golden Eagle Snatches Kid",
"publishDate" : "2012-12-19T13:55:28Z",
"url" : "https://www.youtube.com/watch?v=Xb0P5t5NQWM"
}
但它会是:
{
"title" : "Golden Eagle Snatches Kid",
"publishDate" : "2012-12-19T13:55:28Z",
"url" : "https://www.youtube.com/watch?v=Xb0P5t5NQWM",
"_links" : {
"self" : {
"href" : "http://localhost/youTubeVideos/Xb0P5t5NQWM"
},
"youTubeVideo" : {
"href" : "http://localhost/youTubeVideos/Xb0P5t5NQWM{?projection}",
"templated" : true
},
"user" : {
"href" : "http://localhost/youTubeVideos/Xb0P5t5NQWM/user"
}
}
}
【问题讨论】:
标签: java spring rest spring-data-rest spring-hateoas