json 带有@requestbody 的 Spring mvc 错误请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22102697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring mvc bad request with @requestbody
提问by colymore
i have a Spring MVC service with that signature:
我有一个带有该签名的 Spring MVC 服务:
@RequestMapping(method = RequestMethod.POST, value = "/addUser", consumes = "application/json")
public @ResponseBody User addUser(@RequestBody User user) {
And this in context.xml
这在 context.xml 中
<bean id="HymansonMessageConverter"
class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="HymansonMessageConverter" />
</list>
</property>
</bean>
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:
我做了一个 Post 请求并且总是返回一个 400error->Bad 请求。我写了一个过滤器来读取请求内容,是这样的:
Edit json:
编辑json:
{
"email": "Anchor",
"latitude": 40.3139461,
"longitude": -3.8810229,
"name": "a",
"online": true,
"password": "a",
"deviceRegId": "APA91bGnD1EuqEm9cpoHsenC-HEphQJRniEnhPovK24QkKkLBXrDesSCP6CFlyOKwR1huwSI28Wd-DdN0N8MDKle7myYB7Dznzc3Z11ZOv3jMlJEIegykpnnnYScrElw2czQEa4pKFeQW7BklUsUS-IB15LMqH_Ag"
}
Edit: The user class
编辑:用户类
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;
}
Whats the problem?
有什么问题?
回答by Touchstone
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.
因为在数据绑定时调用了默认构造函数。
Check your json data:
检查您的 json 数据:
{
"email": "Anchor",
"latitude": 40.3139461,
"longitude": -3.8810229,
"name": "a",
"online": true,
"password": "a",
"deviceRegId": "APA91bGnD1EuqEm9cpoHsenC-HEphQJRniEnhPovK24QkKkLBXrDesSCP6CFlyOKwR1huwSI28Wd-DdN0N8MDKle7myYB7Dznzc3Z11ZOv3jMlJEIegykpnnnYScrElw2czQEa4pKFeQW7BklUsUS-IB15LMqH_Ag"
}
Verify the following things:
验证以下事项:
- Whether the nameof JSON matches the User class's field name.
Also check whether JSON valueis supported by the corresponding field name datatypein User class.
Do try it, I have faced the same issue numerous times.
- JSON的名称是否与 User 类的字段名称匹配。
还要检查User 类中对应的字段名称数据类型是否支持JSON值。
试试吧,我遇到过很多次同样的问题。
回答by Brian T.
I was going to guess the problem is with consumes = "application/json", and maybe it is... but I did a quick test adding that restriction to my environment (which is not specifying that it is sending "application/json"),
我想猜测问题出在consumes = "application/json",也许是……但我做了一个快速测试,将这个限制添加到我的环境中(没有指定它正在发送"application/json"),
and instead I got a "415 Unsupported media Type".
相反,我得到了“415 Unsupported media Type”。
Still, I recommend removing that and see if it helps.
不过,我建议删除它,看看它是否有帮助。
回答by Eruvanos
I had a bad request too. And sometimes bad syntax etc. But I fixed it with another Hymanson Dependancy:
我也有一个不好的要求。有时语法错误等。但我用另一个 Hymanson Dependancy 修复了它:
Instead of Codehause Hymanson, I use now fasterxml:
我现在使用 fastxml 代替 Codehause Hymanson:
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.3.3</version>
</dependency>

