我有弹簧控制器:
@RequestMapping(value = "/add", method = RequestMethod.POST,
consumes = "application/json")
public @ResponseBody ResponseDto<Job> add(User user) {
...
}
我可以使用 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);
在控制器中,我得到名为“xxx”的用户
现在我想创建 User 对象并将其发布到服务器,我尝试像这样使用 GSON 对象:
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);
但是通过这种方式,我进入了带有空字段的服务器用户对象......
我该如何解决?