如果我有这样的内容:
@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服务(在Content字段中)?是不是这样的:
{"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);
}