PUT method with @FormParam

If I have something like:

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

How can I enter the both parameters to Test my REST services (in the Content field)? Isn't something like:

{"login":"xxxxx","password":"xxxxx"}

Thanks


Form parameter data will only be present when you submit ... form data. Change the @Consumes type for your resource to 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;
}

Then on your client side, set:

  • Content-Type: multipart/form-data
  • Add form variables for login and password
  • On a side note, assuming this isn't for learning, you will want to secure your login endpoint with SSL, and hash the password before sending it across the wire.


    EDIT

    Based on your comment, I am including an example of sending a client request with the required form data:

    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/45360.html

    上一篇: 在REST代理和/或存储中访问自定义响应头?

    下一篇: 使用@FormParam的PUT方法