What to return if Spring MVC controller method doesn't return value?
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
Spring MVC controller method called for GET but not for POST
newer spring security versions enable csrf by default which leads to strange errors sometimes. Try disabling by .csrf().disable()
in your security config or look here: https://www.baeldung.com/spring-security-csrf
Return only string message from Spring MVC 3 Controller
Annotate your method in controller with @ResponseBody
:
@RequestMapping(value="/controller", method=GET)
@ResponseBody
public String foo() {
return "Response!";
}
From: 15.3.2.6 Mapping the response body with the @ResponseBody
annotation:
The
@ResponseBody
annotation [...] can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
What is the best way to return different types of ResponseEntity in Spring-Boot (Error Handling for REST with Spring)
I recommend using Spring's @ControllerAdvice
to handle errors. Read this guide for a good introduction, starting at the section named "Spring Boot Error Handling". For an in-depth discussion, there's an article in the Spring.io blog that was updated on April, 2018.
A brief summary on how this works:
- Your controller method should only return
ResponseEntity<Success>
. It will not be responsible for returning error or exception responses. - You will implement a class that handles exceptions for all controllers. This class will be annotated with
@ControllerAdvice
- This controller advice class will contain methods annotated with
@ExceptionHandler
- Each exception handler method will be configured to handle one or more exception types. These methods are where you specify the response type for errors
- For your example, you would declare (in the controller advice class) an exception handler method for the validation error. The return type would be
ResponseEntity<Error>
With this approach, you only need to implement your controller exception handling in one place for all endpoints in your API. It also makes it easy for your API to have a uniform exception response structure across all endpoints. This simplifies exception handling for your clients.
Spring MVC- returning string value from controller
Just change your controller code as below
@RequestMapping("/greet")
public @ResponseBody String user(User user) {
return "Hi User";
}
See documentation of ResponseBody here
Related Topics
How to Convert .Pfx File to Keystore with Private Key
How to Convert a Binary String to a Base 10 Integer in Java
Static Block VS. Initializer Block in Java
Creating Animated Gif with Imageio
When Not to Use the Static Keyword in Java
What Is the Idiomatic Way to Compose a Url or Uri in Java
How to Read Text from Hidden Element with Selenium Webdriver
What's an Example of Duck Typing in Java
Can Hibernate Work with MySQL's "On Duplicate Key Update" Syntax
In Java 8, Why Is the Default Capacity of Arraylist Now Zero
How to Enable Anti Aliasing in Arbitrary Java Apps
Jackson/Hibernate, Meta Get Methods and Serialization
Java Threads and Number of Cores
"Noclassdeffounderror: Could Not Initialize Class" Error
How to Compile Multiple Java Source Files in Command Line
Javafx and Maven: Nullpointerexception: Location Is Required