154

I am using jQuery's $.getJSON() to make asynchronous calls to my simple Spring MVC backend. Most of the Spring controller methods look like this:

@RequestMapping(value = "/someURL", method = RequestMethod.POST)
public @ResponseBody SomePOJO getSomeData(@ModelAttribute Widget widget,
    @RequestParam("type") String type) {
    return someDAO.getSomeData(widget, type);
}   

I have things set up so that each controller returns the @ResponseBody as JSON, which is what the client-side expects.

But what happens when a request isn't supposed to return any content to the client-side? Can I have:

@RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
public @ResponseBody void updateDataThatDoesntRequireClientToBeNotified(...) {
    ...
}

If not, what's the appropriate syntax to use here?

3
  • I assume if you don't return anything, there will not be any content sent back?
    – arahant
    Commented Oct 11, 2012 at 11:28
  • 1
    I think I'd still return a POJO of some sort, even if in Version 1 of your solution it just wraps a "success" boolean or something similar. Then you've got a consistent pattern in all your AJAX methods, and something that's easier to build on when it turns out you do need to return something!
    – millhouse
    Commented Oct 11, 2012 at 11:32
  • Contrary to what the answers are suggestion, what you first had in your second snippet is perfectly fine and the correct way to handle POST data.
    – Brett Ryan
    Commented Dec 19, 2013 at 4:36

8 Answers 8

283

you can return void, then you have to mark the method with @ResponseStatus(value = HttpStatus.OK) you don't need @ResponseBody

@RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void updateDataThatDoesntRequireClientToBeNotified(...) {
    ...
}

Only get methods return a 200 status code implicity, all others you have do one of three things:

  • Return void and mark the method with @ResponseStatus(value = HttpStatus.OK)
  • Return An object and mark it with @ResponseBody
  • Return an HttpEntity instance
7
  • 33
    Actually, you do not need to set @ResponseStatus and shouldn't. Simply having @ResponseBody on a void handler is fine enough.
    – Brett Ryan
    Commented Dec 19, 2013 at 4:33
  • 13
    I think it will be better to return a 204 No Content instead of a 200 for void methods
    – raspacorp
    Commented Sep 15, 2014 at 15:56
  • 2
    @raspacorp 200 is correct for POST as it's not meant to have a body.
    – Brett Ryan
    Commented Oct 20, 2014 at 16:53
  • 8
    @BrettRyan just as a comment, at least for a REST API it is a common practice that a POST will be used for creating content in which case it usually returns the id of the created enity(s), the full created entities or a link to the read operation. A 200 status return with no content could be confusing from a REST API perspective.
    – raspacorp
    Commented Oct 20, 2014 at 20:45
  • 2
    @ams If i am not using both (@ResponseBody AND @ResponseStatus(value = HttpStatus.OK)) the option then what would be happen??? Commented Aug 11, 2017 at 11:26
46

You can simply return a ResponseEntity with the appropriate header:

@RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
public ResponseEntity updateDataThatDoesntRequireClientToBeNotified(...){
....
return new ResponseEntity(HttpStatus.OK)
}
4
  • Incase anyone ran into the same problem I did, this didn't work on an older version of spring (4.1.1) I would get 500 errors. I upgraded to 4.2.0 and this works great
    – sauce
    Commented Aug 18, 2015 at 19:24
  • That's my prefered way of returning empty 200 as well. Since Spring 4.1 use the builder pattern instead: return ResponseEntity.ok().build(); Commented Jan 30, 2018 at 10:01
  • 6
    Although seems to compile, it gives the following warning ResponseEntity is a raw type. References to generic type ResponseEntity<T> should be parameterized
    – Gonzalo.-
    Commented Feb 11, 2019 at 20:32
  • I use ResponseEntity<?> as return type. No warnings, any response type can be returned
    – tequilacat
    Commented Mar 10, 2021 at 1:19
10

You can return "ResponseEntity" object. Using "ResponseEntity" object is very convenient both at the time of constructing the response object (that contains Response Body and HTTP Status Code) and at the time of getting information out of the response object.

Methods like getHeaders(), getBody(), getContentType(), getStatusCode() etc makes the work of reading the ResponseEntity object very easy.

You should be using ResponseEntity object with a http status code of 204(No Content), which is specifically to specify that the request has been processed properly and the response body is intentionally blank. Using appropriate Status Codes to convey the right information is very important, especially if you are making an API that is going to be used by multiple client applications.

1
  • 3
    setting @ResponseStatus(HttpStatus.NO_CONTENT) solved XML Parsing Error: no root element found for me in the browser
    – aliopi
    Commented Aug 31, 2017 at 11:39
4

Yes, you can use @ResponseBody with void return type:

@RequestMapping(value = "/updateSomeData" method = RequestMethod.POST)
@ResponseBody
public void updateDataThatDoesntRequireClientToBeNotified(...) {
    ...
}
2
  • 2
    so what will be the return type.. is it HTTP status code?
    – spandey
    Commented Jan 23, 2018 at 5:30
  • @techBeginner In this case 200 (OK). Commented May 11, 2018 at 9:09
2

But as your system grows in size and functionality... i think that returning always a json is not a bad idea at all. Is more a architectural / "big scale design" matter.

You can think about returing always a JSON with two know fields : code and data. Where code is a numeric code specifying the success of the operation to be done and data is any aditional data related with the operation / service requested.

Come on, when we use a backend a service provider, any service can be checked to see if it worked well.

So i stick, to not let spring manage this, exposing hybrid returning operations (Some returns data other nothing...).. instaed make sure that your server expose a more homogeneous interface. Is more simple at the end of the day.

2

There is nothing wrong with returning a void @ResponseBody and you should for POST requests.

Use HTTP status codes to define errors within exception handler routines instead as others are mentioning success status. A normal method as you have will return a response code of 200 which is what you want, any exception handler can then return an error object and a different code (i.e. 500).

0

HttpStatus is required.

@ResponseStatus(value = HttpStatus.OK)
public void update(...) {
    ...
}

or ResponseEntity with null body and null header.

public ResponseEntity<Void> delete(@PathVariable Long id) {
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
0

You can use Void class:

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

@GetMapping
public ResponseEntity<Void> method() {
   return ResponseEntity.status(HttpStatus.OK)
          .build();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.