使用@FormParam的PUT方法

如果我有类似的东西:

@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;
}

然后在你的客户端,设置:

  • 内容类型:multipart / form-data
  • loginpassword添加表单变量
  • 请注意,假设这不是用于学习的,您需要使用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);
    }
    
    链接地址: http://www.djcxy.com/p/45359.html

    上一篇: PUT method with @FormParam

    下一篇: RS, what is the common usage for @QueryParam and @Consume?