gpt4 book ai didi

json - Spring mvc 与 @requestbody 的错误请求

转载 作者:行者123 更新时间:2023-12-02 01:35:57 25 4
gpt4 key购买 nike

我有一个具有该签名的 Spring MVC 服务:

@RequestMapping(method = RequestMethod.POST, value = "/addUser", consumes = "application/json")
public @ResponseBody User addUser(@RequestBody User user) {

这在 context.xml 中

<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>

我发出一个 Post 请求,但总是返回 400error->Bad request。我写了一个过滤器来读取请求内容,是这样的:

编辑 json:

{
"email": "Anchor",
"latitude": 40.3139461,
"longitude": -3.8810229,
"name": "a",
"online": true,
"password": "a",
"deviceRegId": "APA91bGnD1EuqEm9cpoHsenC-HEphQJRniEnhPovK24QkKkLBXrDesSCP6CFlyOKwR1huwSI28Wd-DdN0N8MDKle7myYB7Dznzc3Z11ZOv3jMlJEIegykpnnnYScrElw2czQEa4pKFeQW7BklUsUS-IB15LMqH_Ag"
}

编辑:用户类别

public class User implements Serializable{


@JsonProperty("deviceRegId")
private java.lang.String deviceRegistrationID;
@JsonProperty("email")
private java.lang.String email;
@JsonProperty("latitude")
private java.lang.Double latitude;
@JsonProperty("longitude")
private java.lang.Double longitude;
@JsonProperty("name")
private java.lang.String name;
@JsonProperty("online")
private java.lang.Boolean online;
@JsonProperty("password")
private java.lang.String password;

public User(String deviceRegid) {
this.deviceRegistrationID = deviceRegid;
this.online = true;
}

public java.lang.String getDeviceRegistrationID() {
return deviceRegistrationID;
}

public java.lang.String getEmail() {
return email;
}

public void setEmail(java.lang.String email) {
this.email = email;
}

public java.lang.Double getLatitude() {
return latitude;
}

public void setLatitude(java.lang.Double latitude) {
this.latitude = latitude;
}

public java.lang.Double getLongitude() {
return longitude;
}

public void setLongitude(java.lang.Double longitude) {
this.longitude = longitude;
}

public java.lang.String getName() {
return name;
}

public void setName(java.lang.String name) {
this.name = name;
}

public java.lang.Boolean getOnline() {
return online;
}

public void setOnline(java.lang.Boolean online) {
this.online = online;
}

/**
* @return value or {@code null} for none
*/
public java.lang.String getPassword() {
return password;
}

/**
* @param password
* password or {@code null} for none
*/
public void setPassword(java.lang.String password) {
this.password = password;
}

有什么问题吗?

最佳答案

请删除参数化构造函数,然后就可以了。 :)

public User(String deviceRegid) {
this.deviceRegistrationID = deviceRegid;
this.online = true;
}

因为在数据绑定(bind)时调用默认构造函数。

检查您的 json 数据:

{
"email": "Anchor",
"latitude": 40.3139461,
"longitude": -3.8810229,
"name": "a",
"online": true,
"password": "a",
"deviceRegId": "APA91bGnD1EuqEm9cpoHsenC-HEphQJRniEnhPovK24QkKkLBXrDesSCP6CFlyOKwR1huwSI28Wd-DdN0N8MDKle7myYB7Dznzc3Z11ZOv3jMlJEIegykpnnnYScrElw2czQEa4pKFeQW7BklUsUS-IB15LMqH_Ag"
}

验证以下事项:

  1. JSON 的名称是否与 User 类的字段名称匹配。

  2. 同时检查 User 类中相应字段名称 datatype 是否支持 JSON value

    请尝试一下,我已经多次遇到同样的问题。

关于json - Spring mvc 与 @requestbody 的错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22102697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com