假设我有一个User对象:
public class User {
private Long id;
private String name;
private String surname;
// getters + setters
}
通过SOAP创建用户时,我希望用户只输入名称和姓氏,而不是id。在这种情况下,我应该创建类似UserDTO对象的东西吗?例如:
public class UserDTO {
private String name;
private String surname;
// getters + setters
}
在我的服务bean中,我将使用以下方法:
public void updateUser (Long id, UserDTO dto) {
User user = new User(id, dto.getName(), dto.getSurname());
service.updateUser(user);
}
这对我的问题是否正确?谢谢
答案 0 :(得分:0)
我不确定我是否理解你的问题,但为什么不为你的User
课提供第二个构造函数呢?
它将是:
public class User {
private Long id;
private String name;
private String surname;
//in case of creating user manually
public User(Long id, String name, String surname) {
this.id = id;
this.name = name;
this.surname = surname;
}
//in case of creating user via soap:
public User(String name, String surname) {
this.id = generateId(); //or some other logic to specify users id
this.name = name;
this.surname = surname;
}
}