@RequestBody 未将 JSON 映射到 Java 对象 - Spring 启动
@RequestBody not mapping JSON to Java Object - Spring Boot
我无法将 JSON 从 post 的方法主体转换为我的 POJO,@RequestBody
在我的控制器 class.
中
我调试了错误,发现某些字段已映射,而其他字段未映射。像这样 (POJO):
name: null, typeOfPlan: null, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: null, password: 1234
,奇怪。
JSON:
{
"confirmPassword": "1234",
"email": "example@gmail.com",
"password": "1234",
"phoneNum": "123456789",
"name": "Hello world",
"typeOfPlan": "Test",
"userName": "user",
"website": "test.org"
}
控制器
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);
}
}
从这里,我调用我的服务,然后是 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;
}
我也试过在 @PostMapping
中允许 JSON 和 consumes media type
,但是没能解决这个问题。
使用 Jackson ObjectMapper 效果不佳。
首先,如果您正在使用 Postman, curl etc..
,请尝试以这种方式发送 json:
{
"confirmPassword": "1234",
"email": "example@gmail.com",
"password": "1234",
"phoneNum": "123456789",
"name": "Hello world",
"typeOfPlan": "Test",
"userName": "user",
"website": "test.org"
}
其次,我得到的唯一空值是 username
,因为您在 json 中像这样发送它:userName
,您应该检查您的 json 与您的 POJO
兼容
我认为问题出在 content-type 上!将其设置为 application/json
。在 postman 或 curl 中指定。
如果你用js发送它试试这个例子
axios.post(
"/rest/yoururl",
jsObject
).then(
function (response) {
console.log(response.data);
}.bind(this)
);
默认情况下 content-type 是 json
卷曲:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://url
在邮递员中:
Headers-> put key Content-Type -> value application/json. If you are
in postman
我的问题很简单: 我的 Angular 项目中的变量,将数据发送到我的 Spring 启动应用程序拼写错误,因此不是被我的后端应用程序识别,因此没有正确映射到我的 POJO。
在我更改前端表单变量以匹配我的 POJO 变量后,我得到了这个:
POJO 数据
name: It's good now, typeOfPlan: 2 Year, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: Master, password: 1234
Spring 引导无法从 JSON 映射 name
、typeOfPlan
和 Username
,因为它们根本不匹配我后端中的那些.
之前
Name, typeOfPlan, userName
之后
name, type, username
谢谢大家!
在我的例子中,我将数据包裹在这样的括号中 - {data}。很好地仔细检查结构是数组还是对象。
我无法将 JSON 从 post 的方法主体转换为我的 POJO,@RequestBody
在我的控制器 class.
我调试了错误,发现某些字段已映射,而其他字段未映射。像这样 (POJO):
name: null, typeOfPlan: null, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: null, password: 1234
,奇怪。
JSON:
{
"confirmPassword": "1234",
"email": "example@gmail.com",
"password": "1234",
"phoneNum": "123456789",
"name": "Hello world",
"typeOfPlan": "Test",
"userName": "user",
"website": "test.org"
}
控制器
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);
}
}
从这里,我调用我的服务,然后是 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;
}
我也试过在 @PostMapping
中允许 JSON 和 consumes media type
,但是没能解决这个问题。
使用 Jackson ObjectMapper 效果不佳。
首先,如果您正在使用 Postman, curl etc..
,请尝试以这种方式发送 json:
{
"confirmPassword": "1234",
"email": "example@gmail.com",
"password": "1234",
"phoneNum": "123456789",
"name": "Hello world",
"typeOfPlan": "Test",
"userName": "user",
"website": "test.org"
}
其次,我得到的唯一空值是 username
,因为您在 json 中像这样发送它:userName
,您应该检查您的 json 与您的 POJO
我认为问题出在 content-type 上!将其设置为 application/json
。在 postman 或 curl 中指定。
如果你用js发送它试试这个例子
axios.post(
"/rest/yoururl",
jsObject
).then(
function (response) {
console.log(response.data);
}.bind(this)
);
默认情况下 content-type 是 json
卷曲:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://url
在邮递员中:
Headers-> put key Content-Type -> value application/json. If you are in postman
我的问题很简单: 我的 Angular 项目中的变量,将数据发送到我的 Spring 启动应用程序拼写错误,因此不是被我的后端应用程序识别,因此没有正确映射到我的 POJO。
在我更改前端表单变量以匹配我的 POJO 变量后,我得到了这个:
POJO 数据
name: It's good now, typeOfPlan: 2 Year, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: Master, password: 1234
Spring 引导无法从 JSON 映射 name
、typeOfPlan
和 Username
,因为它们根本不匹配我后端中的那些.
之前
Name, typeOfPlan, userName
之后
name, type, username
谢谢大家!
在我的例子中,我将数据包裹在这样的括号中 - {data}。很好地仔细检查结构是数组还是对象。