Reputation: 12346
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) {
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>
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:
Edit 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?
Upvotes: 10
Views: 29544
Reputation: 689
I had a bad request too. And sometimes bad syntax etc. But I fixed it with another Jackson Dependancy:
Instead of Codehause Jackson, I use now fasterxml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
Upvotes: 0
Reputation: 5982
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:
{
"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 name of JSON matches the User class's field name.
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.
Upvotes: 25
Reputation: 1
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"
),
and instead I got a "415 Unsupported media Type".
Still, I recommend removing that and see if it helps.
Upvotes: 0