1. Overview

In this tutorial, we'll explore different ways to set the response status using Spring MVC.

2. Default Status Codes

By default, Spring MVC returns 200 - OK for the GET, POST, PUT, DELETE and PATCH requests in case of a successful result.

@RestController
public class ResponseStatusRestController {

    @GetMapping("/status")
    public String status() {
        return "Done";
    }

    @PostMapping("/statusPost")
    public String statusPost() {
        return "Done";
    }

    @PutMapping("/statusPut")
    public String statusPut() {
        return "Done";
    }

    @DeleteMapping("/statusDelete")
    public String statusDelete() {
        return "Done";
    }

    @PatchMapping("/statusPatch")
    public String statusPatch() {
        return "Done";
    }
}

3. Using ResponseEntity

ResponseEntity allows us to define the response status in a controller method.

@PostMapping("/statusWithResponseEntity")
public ResponseEntity<String> statusWithResponseEntity() {
    return ResponseEntity.status(HttpStatus.ACCEPTED).body("Done");
}

In this endpoint, we're setting the status code as HttpStatus.ACCEPTED. In general, the ResponseEntity class gives us more control over the response in that we can set the headers, status and response body.

4. Using HttpServletResponse

Alternatively, we can use HttpServletResponse to set the response status. For this purpose, we must first add HttpServletResponse as a parameter to our controller method.

@PostMapping("/statusWithResponse")
public String statusWithResponse(HttpServletResponse servletResponse) {
    servletResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
    return "Done";
}

Here, we're calling the setStatus method with a status code.

5. Using @ResponseStatus

Lastly, Spring MVC provides the @ResponseStatus annotation which we can use in different places.

5.1. On Method

We'll first use @ResponseStatus on a controller method to set its status code:

@ResponseStatus(HttpStatus.ACCEPTED)
@PostMapping("/statusWithAnnotation")
public String statusWithAnnotation() {
    return "Done";
}

The response status will be 202 - Accepted when the result is a success.

5.2. On Controller

@ResponseStatus can also be used on the controller level. In this way, all endpoints in that controller return the class level responses status. However if the endpoint method also contains a @ResponseStatus annotation, its value overrides the former.

@ResponseStatus(HttpStatus.ACCEPTED)
@RestController
@RequestMapping("/controller")
public class ControllerLevelResponseRestController {

    @PostMapping("/status")
    public String status() {
        return "Done";
    }

    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/statusOverride")
    public String statusWithAnnotation() {
        return "Done";
    }
}

Here, ControllerLevelResponseRestController defines the response status as 202 - Accepted. Thus the /status endpoint returns 202. However, /statusOverride returns 201 since we're annotating it with @ResponseStatus(HttpStatus.CREATED).

5.3. On @ExceptionHandler

Another place we can use @ResponseStatus is @ExceptionHandler methods:

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public void handleRuntimeException(RuntimeException e) {
    // Implementation details...
}

6. Summary

In this tutorial, we've investigated how we can set the response status using Spring MVC.

As always the source code for all examples is available on Github.