gpt4 book ai didi

java - ReSTLet:使用多个参数发布

转载 作者:行者123 更新时间:2023-12-01 12:44:14 26 4
gpt4 key购买 nike

请看下面的示例:

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");
}

输出是:
用户名=测试密码
密码=空

我做错了什么?还是不能使用多个参数?

最佳答案

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

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

关于java - ReSTLet:使用多个参数发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com