使用spring @RestController在null上返回HTTP 204
这将返回200 OK,且Content-Length:0
@RestControllerpublic class RepoController {
@RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
public Object getDocument(@PathVariable long id) {
return null;
}
}
简而言之,我希望它在null上返回204 No Content。
有没有办法迫使spring-mvc /
rest返回null而不是200?我不想更改每个rest方法以返回ResponseEntity或类似的东西,仅将null映射到204
回答:
当然可以
@RestControllerpublic class RepoController {
@RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
public Object getDocument(@PathVariable long id, HttpServletResponse response) {
Object object = getObject();
if( null == object ){
response.setStatus( HttpStatus.SC_NO_CONTENT);
}
return object ;
}
}
@RestControllerpublic class RepoController {
@RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
public Object getDocument(@PathVariable long id) {
Object object = getObject();
if ( null == object ){
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return object ;
}
}
可能有错别字,但您会明白。
以上是 使用spring @RestController在null上返回HTTP 204 的全部内容, 来源链接: utcz.com/qa/419555.html