To receive an empty request with Spring Web MVC @RequestBody

@RequestBody has a required attribute whose default value is true.

For example, if the following API is defined (the API that returns the request as it is) and you send an "empty" request to it, the default value will be ʻorg.springframework.http.converter. I get an HttpMessageNotReadableException`.

@PostMapping(path = "/hogehoge",
  consumes = "text/plain; charset=UTF-8",
  produces = "text/plain; charset=UTF-8")
@ResponseBody
public String execute(@RequestBody String body) {
  return body;
}

By setting @RequestBody (required = false), you can handle empty requests as well. By the way, for an empty request, the body will be null.

Recommended Posts

To receive an empty request with Spring Web MVC @RequestBody
Major changes related to Spring Framework 5.0 Web MVC
How to create an Excel form using a template file with Spring MVC
I tried to implement file upload with Spring MVC
Java Config with Spring MVC
Initialization with an empty string to an instance of Java String type
Let's find out how to receive in Request Body with REST API of Spring Boot
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to make an Android application with MVC now (Java)
Create an app with Spring Boot 2
Implement file download with Spring MVC
Create an app with Spring Boot
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
How to read Body of Request multiple times with Spring Boot + Spring Security
Spring5 MVC web application development with Visual Studio Code SQL Server connection
Spring5 MVC Web application development with Visual Studio Code Maven template creation
Send a request to the backend after authenticating with Spring Cloud Gateway
How to bind request parameters in list format with bean property in Spring
How to crop an image with libGDX
Start web application development with Spring Boot
Transition from Struts2 to Spring MVC (Controller)
[Spring MVC] How to pass path variables
Implement image input / output with Spring MVC
Run WEB application with Spring Boot + Thymeleaf
[Error resolution] Occurs when trying to build an environment for spring with docker
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 3/3 [Page creation 2/2]
I tried to clone a web application full of bugs with Spring Boot
From creating a Spring Boot project to running an application with VS Code