5

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Compiler v2
  • 3,509
  • 10
  • 31
  • 55

4 Answers4

2

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!

Compiler v2
  • 3,509
  • 10
  • 31
  • 55
  • 1
    I actually had the same issue, and I can't believe I didn't spot it. I had "password" with 3 's'es in my request and my eyes just couldn't see it: "passsword". I'm glad you posted this, because it made me go back and double-check. – Marnee Mar 05 '21 at 21:11
0

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

Daniel Taub
  • 5,133
  • 7
  • 42
  • 72
  • No, I am not using Postman or Curl. I am sending it directly from my frontend Angular project. – Compiler v2 Jun 28 '19 at 17:50
  • I wasn't sending my JSON with a null value, I don't understand what you are trying to say. – Compiler v2 Jun 28 '19 at 18:07
  • I meant the that userName have a capital N inside and because of the it didnt worked – Daniel Taub Jun 28 '19 at 18:08
  • I see, but I got the JSON from my chrome inspector that outputs the `console.log()`. So it shouldn't really affect my JSON, because it is just a representation the JSON. I never touched it. – Compiler v2 Jun 28 '19 at 18:16
0

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

Alex
  • 3,923
  • 3
  • 25
  • 43
  • Where do I find `content-type` so I can set it to `application/json`? – Compiler v2 Jun 28 '19 at 17:59
  • Headers-> put key `Content-Type` -> value `application/json`. If you are in postman – Alex Jun 28 '19 at 18:01
  • I am clueless where to put it in my project, again I don't have postman nor Curl, or any other app that deals with what you are saying. I sent the JSON manually by typing into the form from my frontend project and submitted it. – Compiler v2 Jun 28 '19 at 18:12
  • ops, that is problem. Check what is Content-type in browser then – Alex Jun 28 '19 at 18:15
  • Where is that in my browser? I cannot find it as said here: https://stackoverflow.com/questions/15148497/in-chrome-whats-the-simplest-way-to-view-the-mime-type-of-a-document I am looking at my network tab in the inspector. – Compiler v2 Jun 28 '19 at 18:21
  • in chrome: right mouse click -> inspect element -> network tab -> when you press a button on your form, some request will be sent and will apear in log -> click on it -> look at its headers -> done – Alex Jun 28 '19 at 20:53
  • I think you need to read something about content types, to be able to use it in your work. It is okey that you aren't front-end developer, but you write code to be used by front-end, and you need to know how it can transmit data to the server, and actually there are only couple popular content-types, you will easily remember them (some are used by default in forms, others used by default when you make ajax request with specific method). https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST – Alex Jun 28 '19 at 21:06
0

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.