如何使用HttpPost传递Object之类的参数

时间:2014-11-05 12:08:34

标签: parameters http-post apache-httpcomponents

我正在使用这种方法的RESTful Web服务:

@RequestMapping(value = "/rest/secure/Userprofile/{providerId}", method = RequestMethod.POST)
@ResponseBody 
public List<Userprofile> addUserprofile(@RequestBody Object[] socialAccounts, @PathVariable String providerId, HttpServletRequest request, HttpServletResponse response) {
       System.out.println("do something!!!");
}

我想传递socialAccount Object。

    String json = "{\"id\":\"26651480000\",\"selected\":true,\"category\":\"Software\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"provideruserid\":\"1475334273\",\"name\":\"Eran\",\"useraccesstoken\":\"CAABletmsJHgBANXAhAlaQXVN1TrI5Tq8gvU002Ke8ZB2dcAhoo21u8orrHiT77G3cm6CmJ4zBX2mu8koeq\",\"checked\":false,\"access_token\":\"CAAB0000wcuq8O\"}";

    CloseableHttpClient httpclient = HttpClients.createDefault();
    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost("localhost:8080/AppDev")
    .setPath("/rest/secure/Userprofile/facebook/");
    URI uri = builder.build();

    HttpPost httppost = new HttpPost(uri);
    ArrayList<NameValuePair> postParameters;
    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("socialAccounts", json));
    httppost.setEntity(new UrlEncodedFormEntity(postParameters));       
    httppost.setHeader("X-Auth-Token", userLogged.getToken());
    httppost.addHeader("Content-Type", "application/json;charset=UTF-8");
    CloseableHttpResponse response = httpclient.execute(httppost);

我明白了:HTTP / 1.1 400 Bad Request

一些建议?

2 个答案:

答案 0 :(得分:0)

我无法发表评论,所以我必须把它作为答案。 也不确定,如果这将解决您的问题 但实际上,你应该更换电话:

CloseableHttpResponse response = httpclient.execute(httpget);

CloseableHttpResponse response = httpclient.execute(httppost);

答案 1 :(得分:0)

有效。

CloseableHttpClient httpclient = HttpClients.createDefault();
        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("localhost:8080/AppDev")
        .setPath("/rest/secure/Userprofile/facebook/");
        URI uri;
        try {
            uri = builder.build();

            HttpPost http = new HttpPost(uri);
            http.setHeader("X-Auth-Token", userLogged.getToken());
            http.addHeader("content-type", "application/json");

            String json = "[{\"category\":\"Public figure\",\"category_list\":[{\"id\":\"1756452480085\",\"name\":\"Catholic Church\"}],\"name\":\"Pope Francesco fans\",\"access_token\":\"token1\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"id\":\"346277202148996\",\"checked\":false,\"selected\":true,\"provideruserid\":\"1475334273\",\"useraccesstoken\":\"usertoken\"},{\"category\":\"Shopping/retail\",\"category_list\":[{\"id\":\"2006019953504\",\"name\":\"Shopping & Retail\"}],\"name\":\"fanpage\",\"access_token\":\"token2\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"id\":\"535193443194952\",\"checked\":false,\"selected\":true,\"provideruserid\":\"1475334273\",\"useraccesstoken\":\"usertoken2\"}]";

            StringEntity params = new StringEntity(json);
            http.setEntity(params);
            CloseableHttpResponse response = httpclient.execute(http);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String json1 = EntityUtils.toString(entity);

                    System.out.println(json1);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }