Geek
Geek

Reputation: 1344

@RequestBody is getting null values

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

Upvotes: 74

Views: 142618

Answers (19)

mubashir
mubashir

Reputation: 731

In my case I had to add a custom constructor with all the values that were coming in JSON object.

Upvotes: 0

Akwaboah Isaac
Akwaboah Isaac

Reputation: 1

Well I got to know I was submitting the request body in UPPER CASE, I change them to lower case and everything works fine for me

Upvotes: 0

beton
beton

Reputation: 41

1-Make Entity class properties start with lowercase. 2-Check for Annotations. 3-Check for Constructor--> **Entity classes should have two constructor. 4-Check for Getter and Setters.

Upvotes: 0

vedavyasa k
vedavyasa k

Reputation: 1661

Check the @RequestBody import,

wrong that will cause the problem in most cases.

import io.swagger.v3.oas.annotations.parameters.RequestBody;

to solve problem It should be

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

Upvotes: 148

ARods
ARods

Reputation: 479

If you are using Lombok you need compileOnly and annotationProcessor In my case I missed the 2nd one. So I got all null values

compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'

Upvotes: 0

Master Mind
Master Mind

Reputation: 2406

In my case, date format was given incorrectly

Upvotes: -1

hu zhang
hu zhang

Reputation: 31

see spring PropertyNamingStrategy(UPPER_CAMEL_CASE,LOWER_CAMEL_CASE ,LOWER_CASE etc... defalult SNAKE_CASE). Spring will auto change http contorller class parameter by naming strategy, which may be not consistant with your request json take SNAKE_CASE as a ex, when "myToken" in java controller class, you client should send my_token instead of myToken

Upvotes: 3

LAKIAR
LAKIAR

Reputation: 1

I have been having this issue too, but the best way i solve mine was checking on spaces after the first quotes in every initialization of fields in my json values

Upvotes: 0

Austine Gwa
Austine Gwa

Reputation: 812

Had the same issue but for my case only one field was not being set. A log on the request body object showed it was being recieved as null. deleted getters and setters for the field and autogenerated them using the IDE and all worked fine.

I highly suspect a mismatch in the getter and setter definition can also cause this

Upvotes: 0

Aatif
Aatif

Reputation: 21

Apart from lowerCamelCasing, for me what additionally needed was applying @JsonProperty(value="your expected JSON KEY name") for each of the getter and setter methods and using this operator under the POJO/Bean/DTO class.

Sample Code:

@JsonProperty(value="policyNumber")
public void setPolicyNumber(String policyNumber) {
        this.policyNumber = policyNumber;
 }

Upvotes: 2

Ameen Mohammad
Ameen Mohammad

Reputation: 21

if you are using Lombok Api then there are no Getters and Setters publicly visible or available to the @ResponseBody and @RequestBody annotation. That is why we read the JSON request with null values.

So you need to comment those @Getter, @Setter annotation to Receive JSON response and Read JSON request object and generate the same getters and setters. Restart or Hot Load (using Spring-Boot-Devtools) server and it should work for sure. You can still use your lombok api for other Entities or BO's.

Upvotes: 2

AndroidDev
AndroidDev

Reputation: 59

In my case, empty constructor must be defined.

public MyClass(){}

Upvotes: 0

tufac2
tufac2

Reputation: 778

In my case was a Lombok issue. I removed all the lombok annotations and created the constructor, setter and getter manually.

As an advise, I would also set the JSON to lowercase to follow the convention.

Upvotes: 1

Manu
Manu

Reputation: 647

I had lombok in my pom, and lombok annotations on my bean. I did not properly installed lombok with my STS yet, and had similar issue, my bean was not populated.

When I removed lombok annotations, my bean was properly populated.

Seems like a combination of lomboc not properly installed on STS + lomboc annotations on my bean.

Upvotes: 3

ubuiestubi
ubuiestubi

Reputation: 81

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.

Upvotes: 8

Jorge Moraleda
Jorge Moraleda

Reputation: 381

Use the annotation org.springframework.web.bind.annotation.RequestBody and not org.springframework.web.bind.annotation.ResponseBody

Upvotes: 1

menakshisundaram
menakshisundaram

Reputation: 193

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

Upvotes: 17

Adriano Dib
Adriano Dib

Reputation: 101

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.

Upvotes: 9

AmanSinghal
AmanSinghal

Reputation: 2504

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.

Upvotes: 86

Related Questions