I have spring controller:
@RequestMapping(value = "/add", method = RequestMethod.POST,
consumes = "application/json")
public @ResponseBody ResponseDto<Job> add(User user) {
...
}
I can POST the object like this with APACHE HTTP CLIENT:
HttpPost post = new HttpPost(url);
List nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
In controller I get user with name "xxx"
Now I want to create User object and post it to the server, I tried to use with GSON object like this :
User user = new User();
user.setName("yyy");
Gson gson = new Gson();
String json = gson.toJson(user);
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
But in this way I get in server User object with null fields...
How can I solve it ?
Ok a few things you're missing:
User
to json the same way on the client and server.HttpMessageConverter
for Gson. You can use GsonHttpMessageConverter from spring-android for that.@RequestBody
.User
class with@JsonIgnoreProperties(ignoreUnknown = true)
As far as I know, Spring MVC uses Jackson for JSON parsing and serialization/deserialization, jackson usually expects the for a JSON content which has data for all class properties, except those who are marked with JSON ignore, like below:
So, if you create a instance of User an set only the user name and send this data to server, Jackson will try to deserialize the content to another User object on server side, during the deserialization process he will consider the two mandatory properties login and name, since only name is filled the deserialization is finished and a null reference is returned to the controller.
You have two options: