Restlet:具有多个参数的帖子

时间:2014-07-19 13:21:01

标签: java rest restlet

请查看以下示例:

public interface ILoginResource {

    @Post
    public void login(String username, String password);
}

public class LoginServerResource extends ServerResource implements ILoginResource { 
    @Override
    public void login(String username, String password) {   
        System.out.println("username = " + username);
        System.out.println("password = " + password);
    }   
}

public static void main(String[] args) {
    ClientResource loginResource = new ClientResource(url);
    ILoginResource res = loginResource.wrap(ILoginResource.class);
    res.login("TestUser","TestPassword");
}

输出是:
username = TestPassword
password = null

我做错了什么?或者是否无法使用多个参数?

1 个答案:

答案 0 :(得分:1)

正如您所料,确实不可能在一次调用中传递多个参数,相反,您可以引入这样的消息类:

public class LoginRequest {

    private String username;

    private String password;

    /**
     * For deserialization.
     */
    public LoginRequest() {
    }

    /**
     * @param username
     *            The user's name
     * @param password
     *            The password
     */
    public LoginRequest(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LoginRequest [username=" + username + ", password=" + password + "]";
    }
}

并修改您的资源:

public interface ILoginResource {
    @Post
    public void login(LoginRequest req);
}