2016-04-30 16 views
0

これはSpringアプリケーションです。私はPOSTリクエストを行い、応答を返そうとしています。私は、クライアント側では、このPOSTリクエストからレスポンスオブジェクトを取得する方法は?

@CrossOrigin 
@RequestMapping(value = "/test", method = RequestMethod.POST) 
public 
@ResponseBody 
Test testmethod(@RequestBody Test test) { 
    test.setValue("test"); 
return test; 
} 

を持っているサーバ側で

私はテストオブジェクトを返す必要がありPOSTメソッドを持っています。私はeconに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); 

私の問題は、私は応答からテストオブジェクトを取得するホットわからないです。助けてください。ありがとう!

答えて

0

あなたは、使用しようとすることができます。

String responseAsString = EntityUtils.toString(response.getEntity()); 
+0

しかし、私は、文字列を必要とするテストオブジェクトを必要としません。 – mlhack

+0

jsonを送信して文字列にします。ちょうどいくつかのJSONをObjectコンバータに使う – mariusz2108

関連する問題