0
这是一个Spring应用程序,我试图做一个POST请求并获取响应。如何从POST请求获取响应对象?
在服务器端我有这个
@CrossOrigin
@RequestMapping(value = "/test", method = RequestMethod.POST)
public
@ResponseBody
Test testmethod(@RequestBody Test test) {
test.setValue("test");
return test;
}
在客户端我有POST方法应该返回测试对象。我使用JSON进行编码。
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;
}
而且
Test s = new Test;
Test s=(Test)post("http://localhost:8081/basic-web-app/test",s);
我的问题是,我不知道烫得到响应测试对象。请帮忙。谢谢!
但我并不需要一个字符串,需要一个测试对象。 – mlhack
你发送json,所以它是一个字符串。只需使用一些JSON到Object转换器 – mariusz2108