Java 如何手动创建Spring数据Rest实体响应格式
我正在使用SpringDataREST创建一个RESTfulAPI。我想处理一个异常,返回一个实体表示,就像SpringDataREST存储库(带有HATEOAS链接)生成的实体表示一样。我需要从中返回实体表示的方法如下:Java 如何手动创建Spring数据Rest实体响应格式,java,spring,rest,spring-data-rest,spring-hateoas,Java,Spring,Rest,Spring Data Rest,Spring Hateoas,我正在使用SpringDataREST创建一个RESTfulAPI。我想处理一个异常,返回一个实体表示,就像SpringDataREST存储库(带有HATEOAS链接)生成的实体表示一样。我需要从中返回实体表示的方法如下: @ExceptionHandler(value = {ExistentUGVException.class}) @ResponseBody protected ResponseEntity<UGV> existentUGVHandler(HttpServletRe
@ExceptionHandler(value = {ExistentUGVException.class})
@ResponseBody
protected ResponseEntity<UGV> existentUGVHandler(HttpServletRequest request, HttpServletResponse response, ExistentUGVException ex) {
return new ResponseEntity<UGV>(ex.ugv, HttpStatus.OK);
}
但这将是:
{
"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"
}
}
}
您必须首先将响应转换为资源,然后手动添加链接 应该是这样的:
@ExceptionHandler(value = {ExistentUGVException.class})
@ResponseBody
protected ResponseEntity<Resource<UGV>> existentUGVHandler(HttpServletRequest request, HttpServletResponse response, ExistentUGVException ex) {
final Resource<UGV> resource = getResource(ex.ugv);
return new ResponseEntity<Resource<UGV>>(resource, HttpStatus.OK);
}
public Resource<T> getResource(T object, Link... links) throws Exception {
Object getIdMethod = object.getClass().getMethod("getId").invoke(object);
Resource<T> resource = new Resource<T>(object); // The main resource
final Link selfLink = entityLinks.linkToSingleResource(object.getClass(), getIdMethod).withSelfRel();
String mappingRel = CLASSMAPPING.getMapping(this.getClass());
final Link resourceLink = linkTo(this.getClass()).withRel(mappingRel);
resource.add(selfLink, resourceLink);
resource.add(links);
return resource;
}
@ExceptionHandler(值={ExistentUGVException.class})
@应答器
受保护响应existentUGVHandler(HttpServletRequest请求、HttpServletResponse响应、ExistentUGVException ex){
最终资源=获取资源(例如ugv);
返回新的响应属性(资源,HttpStatus.OK);
}
公共资源getResource(T对象、链接…链接)引发异常{
对象getIdMethod=Object.getClass().getMethod(“getId”).invoke(对象);
资源资源=新资源(对象);//主资源
final Link selfLink=entityLinks.linkToSingleResource(object.getClass(),getIdMethod).withSelfRel();
字符串mappingRel=CLASSMAPPING.getMapping(this.getClass());
最终链接resourceLink=linkTo(this.getClass()).withRel(mappingRel);
添加(selfLink,resourceLink);
资源。添加(链接);
返回资源;
}
看看这里,这里有你需要的一切:虽然这可以从理论上回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。我修改了我的答案,提供示例代码很高兴看到你编辑了你的文章。
@ExceptionHandler(value = {ExistentUGVException.class})
@ResponseBody
protected ResponseEntity<Resource<UGV>> existentUGVHandler(HttpServletRequest request, HttpServletResponse response, ExistentUGVException ex) {
final Resource<UGV> resource = getResource(ex.ugv);
return new ResponseEntity<Resource<UGV>>(resource, HttpStatus.OK);
}
public Resource<T> getResource(T object, Link... links) throws Exception {
Object getIdMethod = object.getClass().getMethod("getId").invoke(object);
Resource<T> resource = new Resource<T>(object); // The main resource
final Link selfLink = entityLinks.linkToSingleResource(object.getClass(), getIdMethod).withSelfRel();
String mappingRel = CLASSMAPPING.getMapping(this.getClass());
final Link resourceLink = linkTo(this.getClass()).withRel(mappingRel);
resource.add(selfLink, resourceLink);
resource.add(links);
return resource;
}