[英]PUT method with @FormParam
如果我有類似的東西:
@PUT
@Path("/login")
@Produces({"application/json", "text/plain"})
@Consumes("application/json")
public String login(@FormParam("login") String login, @FormParam("password") String password) throws Exception
{
String response = null;
response = new UserManager().login(login, password);
return response;
}
如何輸入兩個參數來測試我的REST服務(在“內容”字段中)? 不是這樣的:
{"login":"xxxxx","password":"xxxxx"}
謝謝
表單參數數據僅在您提交...表單數據時出現。 將資源的@Consumes
類型更改為multipart/form-data
。
@PUT
@Path("/login")
@Produces({ "application/json", "text/plain" })
@Consumes("multipart/form-data")
public String login(@FormParam("login") String login,
@FormParam("password") String password) {
String response = null;
response = new UserManager().login(login, password);
return response;
}
然后在您的客戶端上設置:
login
和password
表單變量 附帶說明一下,假設這不是為了學習,您將需要使用SSL保護登錄端點,並在通過網絡發送密碼之前對密碼進行哈希處理。
編輯
根據您的評論,我提供了一個示例,該示例發送帶有所需表單數據的客戶請求:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(BASE_URI + "/services/users/login");
// Setup form data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", "blive1"));
nameValuePairs.add(new BasicNameValuePair("password",
"d30a62033c24df68bb091a958a68a169"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute request
HttpResponse response = httpclient.execute(post);
// Check response status and read data
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String data = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
System.out.println(e);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.