We know HTTP status codes can be used to determine if a request was successfully processed or if an error was generated. However, we may want to fine-tune the HTTP response to give the user more information about what occurred.

The @ResponseStatus annotation can be applied to methods to help with fine-tuning HTTP responses. The annotation accepts parameters for the status code and reason.

This can be used to customize messages in both failure and success responses. Consider the scenario when an admin adds a new book to the collection. When they enter all of the required information and click submit, we can use @ResponseStatus to return an HTTP status showing the request was successful and a reason indicating the entry was created.

@PutMapping(path="/book") @ResponseStatus(code = HttpStatus.CREATED, reason = "Book was successfully added") public string addNewBook(@RequestParam string title) { }



We’ve created a test method that will throw an exception. We’ll discuss more about exceptions in the next exercise. For now, view the details of the exception by requesting the resource using curl localhost:4001/superHeroes/testException.

You can also try modifying the value and reason in InvalidEntryException.java.

As a reminder, here is the list of HttpStatus values in GitHub.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?