Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use @RequestBody with optional body in latest Spring v4

How do I make body optional in REST API calls when using @RequestBody annotation in Spring?

With Spring's latest version, if you use @RequestBody annotation, it makes client to send body all the time without making it optional.

I tried @RequestBody (required=false) but that didn't work & still my request comes as null.

How do I manage to get request converted to proper required object without making body mandatory?

For eg:

@RequestMapping(value="/add/employee", method=RequestMethod.POST)
public void addEmployee(@RequestBody Employee employee){
    // ...
}

Here, I want to add employee using POST but without body. How do I do that? Spring latest version throws error "body missing" if I send empty body...

like image 557
Mike Powell Avatar asked Feb 24 '15 03:02

Mike Powell


People also ask

Can RequestBody be optional?

With Spring's latest version, if you use @RequestBody annotation, it makes client to send body all the time without making it optional.

How do I use RequestBody in spring boot?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

Is @RequestBody required with @RestController?

If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.

Can we use RequestBody and Requestparam together?

nothing. So it fails with 400 because the request can't be correctly handled by the handler method.


3 Answers

@Santosh, I'm not sure which required argument you're referring. Mike already mentioned that he tried using @RequestBody (required=false) and request was still null. May be you can elaborate more & give example.

@Mike, probably you can try to have another separate converter that will serve your purpose.

Note: I noticed same issue with Spring v4.1.6 & Mike could be using that as he mentioned he is using latest version.

like image 192
Jack Dorson Avatar answered Oct 19 '22 17:10

Jack Dorson


You can use java.util.Optional:

@RequestMapping(value="/add/employee", method=RequestMethod.POST)
public void addEmployee(@RequestBody Optional<Employee> employee){
    // ...
}
like image 16
cilf Avatar answered Oct 19 '22 18:10

cilf


I guess you are using spring version above 3.2 as there was a issue with the version. @RequestBody should have a required parameter to allow a request body to be optional

Have a look at following link Spring @RequestBody Anotation

@RequestBody Body takes and argument required which is true by default. Specifying it to false will help you

public abstract boolean required

Whether body content is required. Default is true, leading to an exception thrown in case there is no body content. Switch this to false if you prefer null to be passed when the body content is null.

like image 12
Santosh Joshi Avatar answered Oct 19 '22 18:10

Santosh Joshi