Spring 如何从POST请求中获取响应对象?
这是一个Spring应用程序,我试图发出一个POST请求并得到一个响应 在服务器端我有这个Spring 如何从POST请求中获取响应对象?,spring,apache,http,post,request,Spring,Apache,Http,Post,Request,这是一个Spring应用程序,我试图发出一个POST请求并得到一个响应 在服务器端我有这个 @CrossOrigin @RequestMapping(value = "/test", method = RequestMethod.POST) public @ResponseBody Test testmethod(@RequestBody Test test) { test.setValue("test"); return test; } 在客户端,我有post方法,它应该返回测试对象
@CrossOrigin
@RequestMapping(value = "/test", method = RequestMethod.POST)
public
@ResponseBody
Test testmethod(@RequestBody Test test) {
test.setValue("test");
return test;
}
在客户端,我有post方法,它应该返回测试对象。我使用JSON进行ecoding
public Object post(String url1, Test test) throws IOException, ClassNotFoundException {
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(login);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url1);
StringEntity input = new StringEntity(jsonInString);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
//read the object from the response, how to do that?
//responseObject = ?????
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseObject;
}
及
我的问题是,我不知道如何从响应中获取测试对象。请帮忙。谢谢 您可以尝试使用:
String responseAsString = EntityUtils.toString(response.getEntity());
但我不需要字符串,我需要一个测试对象。你发送json,它就是一个字符串。只需使用一些JSON到对象转换器
String responseAsString = EntityUtils.toString(response.getEntity());