Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return HTTP 204 on null with spring @RestController

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

like image 453
user1606576 Avatar asked Sep 04 '15 11:09

user1606576


People also ask

How do I return 204 status in spring boot?

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.

Can we return view from RestController?

@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 .

What does @RestController do in spring?

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.

What is the return type of RestController?

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 .


2 Answers

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

like image 157
spekdrum Avatar answered Sep 20 '22 18:09

spekdrum


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.

like image 41
Karthik R Avatar answered Sep 22 '22 18:09

Karthik R