【发布时间】:2014-11-05 12:08:33
【问题描述】:
我正在通过这种方法使用 RESTfull 网络服务:
@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 对象。
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 错误请求
一些建议?
【问题讨论】:
-
您应该指定一个
Content-Type标头,以便端点知道如何解码请求的内容(不过我认为这不是答案,只是提示)
标签: parameters http-post apache-httpcomponents