与@requestbody的Spring mvc错误请求 [英] Spring mvc bad request with @requestbody

查看:194
本文介绍了与@requestbody的Spring mvc错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a Spring MVC service with that signature:

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

这在context.xml中

And this in 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请求。
我写了一个过滤器来读取请求内容,这是:

I do a Post request and always return me a 400error->Bad request. I write a filter to read the request content and is this:

编辑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;
}

问题是什么?

推荐答案

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

Please remove the parametrized constructor and there you go. :)

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

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

Because at databinding default constructor is called.

检查您的json数据:

Check your json data:

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

验证以下内容:


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

  2. 同时检查相应字段名称数据类型是否支持JSON 用户类。

  1. Whether the name of JSON matches the User class's field name.
  2. Also check whether JSON value is supported by the corresponding field name datatype in User class.

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

Do try it, I have faced the same issue numerous times.

这篇关于与@requestbody的Spring mvc错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆