Hello guys, if you are wondering how to return different HTTP Status codes from a Spring MVC controller then you have come to the right place. You can use @ResponseCode annotation on Spring MVC to send different HTTP codes to the client as part of the HTTP response. If you are developing REST APIs then you can even @RestControler to do that job. This article will demonstrate how to return different HTTP statuses for a Spring MVC controller. Prior to implementation, all we need to understand what is with the HTTP return types and why we need them. Let's have a quick look into this.
As a developer, all you need to understand is that you must give a better experience to users of your system for better interaction. Letting other developers know, why the error happens in the code is a best practice that is used to develop well-organized software.
Developers that use your service can work more efficiently because the chances of failure are reduced. This is where HTTP status codes come into play, along with a brief message in the body of the response that explains what's going on.
What are common HTTP Codes for Web Developers?
Returning status code with @ResponseStatus in Spring - Example
@Controller
@ResponseBody
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public class StudentController {
@GetMapping("/method")
@ResponseStatus(code = HttpStatus.OK, reason = "OK")
public String ok() {
return "Class Level HTTP Status Overridden. The HTTP Status will be OK (CODE 200)\n";
}
@GetMapping("/error")
public String serviceUnavailable() {
return "The HTTP Status will be SERVICE_UNAVAILABLE (CODE 503)\n";
}
}
Returning status code with an Exception
Now, let's see another example of a controller method to return HTTP status in case of an error or exception. To demonstrate this, we will add the second method to the controller to show how to use an exception to return a status code.
@RequestMapping(value = "/exception", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity sendViaException() {
throw new ForbiddenException();
}
- How Spring MVC works internally?
- 15 Spring Data JPA Interview Questions with Answers
- What is the use of DispatcherServlet in Spring MVC?
- 10 Best Spring Framework Courses for Beginners
- How to implement LDAP authentication in the Active directory
- Top 15 Microservice Interview Questions with Answers
- 10 Advanced Spring Boot Courses for Experienced Developers
- How to use Named Query in Spring?
- 20+ Spring Boot Interview Questions for Java developers
- 11 Free Spring and Spring Boot Courses for Beginners
- Does the order of URLs matter in Spring Security config?
- Top 10 Courses to Learn Microservices with Spring Boot
- 13 Spring Boot Actuator Questions for interviews
- 15 Spring Cloud Interview Questions with Answers
- How to get ServletContext object in Spring controller
- How to enable Spring security in a Java web application?
- How to limit the number of concurrent active sessions in Java web app
- 17 Spring AOP Interview Questions with Answers
Thanks for reading this article so far. If you find this Spring MVC tutorial and HTTP status code example useful, please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.
No comments:
Post a Comment