@RequestBody not mapping JSON to Java Object - Spring Boot

Compiler v2 :

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: [email protected], PhoneNum: 123456789, Website: test.org, Username: null, password: 1234, which is strange.

JSON:

{
    "confirmPassword": "1234",
    "email": "[email protected]",
    "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.

Compiler v2 :

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: [email protected], 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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring @RequestBody not mapping to custom Object

Spring-boot @RequestBody JSON to object with dates deserialization example?

Spring boot @RequestBody default POJO mapping behavior?

JSON to Java Object Mapping , needed for Spring REST

Spring boot RequestBody with object fields that could be needed

Spring Boot save nested Entity with JSON RequestBody

How to bind JSON hasmasp to the @RequestBody in spring boot

How to read additional JSON attribute(s) which is not mapped to a @RequestBody model object in Spring boot

Spring-Boot JPA - Inserting into DB with foreign keys in JSON RequestBody results in null object

Mapping JSON to SQL Columns using Spring Boot / Java

Spring Boot - JSON Object Array to Java Array

Spring Boot, Spring MVC JSON RequestBody: Unknown property ignored

JSON Object mapping - Spring Data

Spring boot parsing @RequestBody

Spring Boot RequestBody with JSONObject

Spring @Requestbody not mapping to inner class

Spring boot rest requestbody and @valid not working when object is null/empty

Do I need a separate object for every @RequestBody in Spring Boot

Requestparam as object in spring boot rest mapping

Spring Boot - Json RequestBody, String/Enum with/without quotation marks

JSON to Java mapping of inner object?

Mapping specific JSON to Java object

Testing JSON mapping for a Spring Boot RestTemplate client

Spring Boot: Json Response is not Mapping to Entity

Spring mapping JSON to java POJO

Spring boot RequestBody getting null

@InitBinder in spring boot not working with @RequestBody

Json request body to java object with spring-boot

how to store json object in a column of mysql using spring boot/java