This returns 200 OK with Content-Length: 0
@RestController public class RepoController { @RequestMapping(value = "/document/{id}", method = RequestMethod.GET) public Object getDocument(@PathVariable long id) { return null; } }
Simply put I'd like it to return 204 No Content on null.
Is there a way to force spring-mvc/rest to return 204 on null not 200? I dont want to change every rest method to return ResponseEntity or something like that, only map null to 204
You can use the @ResponseStatus annotation. This way you can have a void method and you don't have to build a ResponseEntity. BTW returning 200 when the object exists and 204 otherwise it's a bit unusual regarding API REST design. It's common to return a 404 (not found) when the requested object is not found.
@RestController is not meant to be used to return views to be resolved. It is supposed to return data which will be written to the body of the response, hence the inclusion of @ResponseBody .
Spring 4.0 introduced the @RestController annotation in order to simplify the creation of RESTful web services. It's a convenient annotation that combines @Controller and @ResponseBody, which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.
As name suggest, it shall be used in case of REST style controllers i.e. handler methods shall return the JSON/XML response directly to client rather using view resolvers. It is a convenience annotation that is itself annotated with @Controller and @ResponseBody .
You can use the @ResponseStatus annotation. This way you can have a void method and you don't have to build a ResponseEntity.
@DeleteMapping(value = HERO_MAPPING) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void delete(@PathVariable Long heroId) { heroService.delete(heroId); }
BTW returning 200 when the object exists and 204 otherwise it's a bit unusual regarding API REST design. It's common to return a 404 (not found) when the requested object is not found. And this can be achieved using a ControllerAdvice.
In Spring REST it's better to handle Exceptions with a Exception handler instead of putting logic to decide the response status, etc. This is an example using the @ControllerAdvice annotation: http://www.jcombat.com/spring/exception-handling-in-spring-restful-web-service
Of course yes.
Option 1 :
@RestController public 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 ; } }
Option 2 :
@RestController public 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 ; } }
Might have typos, but you get the concept.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With