简体   繁体   English

@RequestBody 未将 JSON 映射到 Java 对象 - Spring Boot

[英]@RequestBody not mapping JSON to Java Object - Spring Boot

I am unable to convert my JSON from post's method body into my POJO, with @RequestBody inside my controller class.我无法将我的 JSON 从 post 的方法主体转换为我的 POJO,在我的控制器类中使用@RequestBody

I debugged the error and I saw that certain fields were mapped and others were not.我调试了错误,我看到某些字段被映射而其他字段没有。 Like this (POJO) :像这样(POJO)

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

JSON: 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.从这里,我调用我的服务,然后调用 DAO 类。

POJO 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.我还尝试在@PostMapping允许 JSON 使用consumes media type ,但它未能解决此问题。

Using Jackson ObjectMapper did not work as well.使用 Jackson ObjectMapper 效果不佳。

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.我的问题很简单:我的 Angular 项目中的变量,将数据发送到我的 Spring Boot 应用程序拼写错误,因此我的后端应用程序无法识别,因此没有正确映射到我的 POJO。


After I changed my frontend form variables to match my POJO's variables, I got this:在更改我的前端表单变量以匹配我的 POJO 变量后,我得到了这个:

POJO data POJO数据

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. Spring Boot 无法从 JSON 映射nametypeOfPlanUsername ,因为它们与我后端的不匹配。


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:首先,如果您使用的是Postman, curl etc..尝试以这种方式发送 json:

{
    "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其次,我得到的唯一空值是username ,因为您在 json 中发送它是这样的: userName ,您应该检查您的 json 是否与您的POJO兼容

I believe the problem is with content-type!我相信问题出在内容类型上! set it to application/json .将其设置为application/json Specify it in postman or curl.在 postman 或 curl 中指定它。

if you send it with js try this example如果你用js发送它试试这个例子

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

by default content-type is json默认内容类型是 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. 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}.在我的情况下,我将数据包裹在这样的括号中 - {data}。 It's nice to double check the structure well whether it is an array or an object.很好地仔细检查结构,无论它是数组还是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM