@RequestBody not mapping JSON to Java Object - Spring Boot

Viewed 16042

I am unable to convert my JSON from post's method body into my POJO, with @RequestBody inside my controller class.

I debugged the error and I saw that certain fields were mapped and others were not. Like this (POJO):

name: null, typeOfPlan: null, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: null, password: 1234, which is strange.

JSON:

{
    "confirmPassword": "1234",
    "email": "example@gmail.com",
    "password": "1234",
    "phoneNum": "123456789",
    "name": "Hello world",
    "typeOfPlan": "Test",
    "userName": "user",
    "website": "test.org"
}

Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SomeController {

    @Autowired
    private Service foo;

    @CrossOrigin
    @PostMapping(value = "/create")
    private void createAccount(@RequestBody BigFoo bigFoo) {
        foo.createAccount(bigFoo);
    }
}

From here, I call my service, then DAO classes.

POJO

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class BigFoo {

    private String name;
    private String typeOfPlan;
    private String email;
    private String website;
    private String phoneNum;
    private String username;
    private String password;
}

I have also tried to allow JSON with consumes media type in the @PostMapping, but it failed it solve this.

Using Jackson ObjectMapper did not work as well.

4 Answers

My problem was simple: my variables in my Angular project, sending the data to my Spring Boot app were misspelled, and therefore were not recognized by my backend application and hence, were not mapped to my POJO correctly.


After I changed my frontend form variables to match my POJO's variables, I got this:

POJO data

name: It's good now, typeOfPlan: 2 Year, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: Master, password: 1234

Spring Boot was unable to map name, typeOfPlan & Username from the JSON because they simply did not match the ones in my backend.


Before

Name, typeOfPlan, userName

After

name, type, username

Thanks all!

First, If you are using Postman, curl etc.. try to send the json that way:

{
    "confirmPassword": "1234",
    "email": "example@gmail.com",
    "password": "1234",
    "phoneNum": "123456789",
    "name": "Hello world",
    "typeOfPlan": "Test",
    "userName": "user",
    "website": "test.org"
}

Second, the only null value I'm getting is username, because you are sending it in your json like that: userName, You should check that your json compatible with your POJO

I believe the problem is with content-type! set it to application/json. Specify it in postman or curl.

if you send it with js try this example

axios.post(
                "/rest/yoururl",
                jsObject
            ).then(
                function (response) {
                    console.log(response.data);
                }.bind(this)
            );

by default content-type is json

by curl:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  http://url

In postMan:

Headers-> put key Content-Type -> value application/json. If you are in postman

In my case I had the data wrapped in brackets like this - {data}. It's nice to double check the structure well whether it is an array or an object.

Related