4 reasons for @RequestBody null issue in Spring Boot

1. Make sure you have first letter as lowercase in all your json request & keys

This is specific to java POJO behaviours only.

Invalid JSON Request:

{
 "CouponId":18,
 "StoreName":"Flipkart",
 "ValidToday":true
}

Valid JSON Request:

{
 "couponId":18,
 "storeName":"Flipkart",
 "validToday":true
}

2. Make sure to import right RequestBody

If you are using Swagger then RequestBody might get imported from Swagger package, so check that and make sure to import RequestBody from here only,

Invalid RequestBody Import

some import from your swagger you can see in your controller.

Valid RequestBody Import:

import org.springframework.web.bind.annotation.RequestBody

3. Make sure you have getters / setters in your RequestBody class

yes this is must.

4. Make sure lombok installation is correct, if you are using lombok

Also make sure you have @Data or @Getter & @Setter in your Request Body class.

2 comments

  • JG

    I was getting same issue which is Request body coming as null value over API Post method. Only difference was my Object class parameters name created as upper characters. I realized that object class parameter names are could be create by lower case character. After I changed it as lower case for all my own class parameter names, it is working fine for me.
    I tried to find out that root cause for 2 days. So this is why i wanted to leave this comment here. I hope, it will helpful for u.

  • Ali Juma Rashidi

    In my case, the JSON properties where defined in snake case [snake_case]. Changed to camel case [camelCase] and worked!

Leave a Reply