import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/path")
@RequestBody
public String message() {
return "hello there!";
}
}
Compilation Error: Annotation interface not applicable to this kind of declaration.
If you are trying to do something like the above, you will get a compilation error "@RequestBody not applicable to method" as the request body annotation does not apply to a method declaration of a @Controller or @RestController.
There could be a few solutions here!
You should be using @ResponseBody here?

If you want to return a serialized HTTP response for this request directly as a response body instead of a view (such as a JSP file) you may have mistakenly used @RequestBody instead of @ ResponseBody.
@Controller
public class MyController {
@RequestMapping("/path")
@ResponseBody
public String message() {
return "hello there!";
}
}
If you use the annotation @RestController you need not need to use @ResponseBody as it is already present in the RestController annotation code.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
Correct way of using @RequestBody in Spring Framework
The correct way to use @RequestBody in Spring is to apply it to a method parameter that you want to bind to the request body of an HTTP request. In other words, you should use @RequestBody when you want to read the content of an incoming HTTP request, such as when processing a POST, PUT, or PATCH request.
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestBody String requestBody) {
return "hello";
}
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- MySQL Query for Schema/Structure of a Table - MySQL
- SCP Copy all files from directory to Local Folder - Linux
- Fix: Microsoft OneDrive We are currently experiencing technical difficulties - Microsoft
- How to Rename a Git Local Branch Using the --move Option - Git
- Docker Commit Command with Examples - Docker
- Move from Zsh to Bash shell macOS - MacOS
- How to run Java Unit Test cases with Apache Maven? - Java
- [Solved] Dynamic Web Module 3.0 requires Java 1.6 or newer Mac OSX - Mac-OS-X