@RequestBody is getting null values

77,686

Solution 1

Try setting the first character of the properties in your JSON to lower case. Eg.

{
    "policyNumber": "123",
    "type": "Test",
    "tenture": "10",
    "sDate": "10-July-2016",
    "hName": "Test User",
    "age": "10"
}

Basically, Spring uses getter and setter to set the properties of the the bean object. And it takes the property of the JSON object, matches it with the setter of the same name. eg to set the policyNumber property it tries to find a setter with the name setpolicyNumber() in your bean class and use that to set the value of your bean object.

Solution 2

Check the @RequestBody import that will cause the problem.

It should be --> import org.springframework.web.bind.annotation.RequestBody;

Solution 3

If you are not in power to change the JSON format and still want to fix this problem, try adding @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class) annotation before your DTO (Policy in example) class.

Solution 4

Setter would have been missed. So, Object values do not get set.

Solution 5

Java convention demands the name of variable in a POJO (attribute of a class) must to be the first character in lowercase.

You have uppercase letters in your JSON properties, which is what is causing the failure.

Share:
77,686
Geek
Author by

Geek

Updated on July 30, 2022

Comments

  • Geek
    Geek almost 2 years

    I have created a simple REST service (POST). But when i call this service from postman @RequestBody is not receiving any values.

    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.ModelAndView;
    
    @RestController
    public class Add_Policy {
        @ResponseBody
        @RequestMapping(value = "/Add_Policy", headers = {
                "content-type=application/json" }, consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
        public Policy GetIPCountry( @RequestBody Policy policy) {
    
            System.out.println("Check value: " + policy.getPolicyNumber());
            return policy;
    
        }
    
    
    }
    

    My java Bean object is like below:

    public class Policy {
        private String PolicyNumber;
        private String Type;
        private String Tenture;
        private String SDate;
        private String HName;
        private String Age;
    
        public String getPolicyNumber() {
            return PolicyNumber;
        }
    
        public void setPolicyNumber(String policyNumber) {
            PolicyNumber = policyNumber;
        }
    
        public String getType() {
            return Type;
        }
    
        public void setType(String type) {
            Type = type;
        }
    
        public String getTenture() {
            return Tenture;
        }
    

    System.out.println is printing a null as a value for PolicyNumber.

    Please help me to resolve this issue.

    JSON which i am passing in request body is

    {
        "PolicyNumber": "123",
        "Type": "Test",
        "Tenture": "10",
        "SDate": "10-July-2016",
        "HName": "Test User",
        "Age": "10"
    }
    

    I have even set Content-Type to application/json in postman

  • mahsa
    mahsa over 4 years
    I had this problem because I forget to create getter and setter.
  • ℛɑƒæĿᴿᴹᴿ
    ℛɑƒæĿᴿᴹᴿ over 4 years
    Put this as a comment
  • R.G
    R.G about 4 years
    Isn't the import same in the question ? Or provide more details to your answer
  • Raw
    Raw almost 4 years
    My problem was the import from swagger not springframework...
  • Leon
    Leon over 3 years
    Yep. For me the import was from swagger. Ugh! Thanks for tip.
  • Panagiss
    Panagiss over 3 years
    i still get that issue. My properties are all lowercase. It gets the object but with all nulls like that Task{id=0, teachingHours=null, surveillanceHours=null, proofReading=null, isActive=false, isValidated=false, candidateId=null, supervisorId=null}
  • kunal
    kunal about 3 years
    For me too the import was from swagger
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • denov
    denov almost 2 years
    same here on swagger getting in the way