How can I prevent an empty JSON object from being deserialized to a Java bean?

Viewed 58

I have a REST controller with a POST mapping. The request body is of type SomeBean and is optional. Currently, an empty JSON object ("{}") is deserialized to a SomeBean instead of null.
What do I need to do so that empty JSON objects are deserialized as null?

Is there a solution with annotations?

public class SomeBean implements Serializable {
  private static final long serialVersionUID = 1L;
  private String prop1;
 
  // getter and setter
  // ..
}
@RestController
@Requestmapping(path = "/service")
public class ServiceRestController {

 @PostMapping
  public Map<String, Serializable> initSession(
      @RequestBody(required = false)
      SomeBean body) {
    // ....
  }

Example curl call:

curl -XPOST -H "Content-type: application/json" -d '{}' 'http://localhost:8080/service'

0 Answers
Related