java - 对于 5 个构造函数参数中的 2 个,Postman 对 spring 的 post 调用始终为 null

标签 java spring rest spring-boot postman

我提前道歉,因为我不确定如何表达这个问题。我有以下类(class):

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;

@Column(name = "username", length = 30)
private String username;

@Column(name = "email", length = 60)
private String email;

@Column(name = "birthday", length = 40)
private Date birthday;

@Column(name = "password", length = 60)
private String password;

@Column(name = "profilepic", length = 60)
private String profilePic;

public User() {
}

public User(String username, String email, String password, Date birthday, String profilePic) {
    this.username = username;
    this.email = email;
    this.password = password;
    this.birthday = birthday;
    this.profilePic = profilePic;
}


public long getId() {
    return id;
}

public String getUsername() {
    return username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getPassword() {
    return password;
}

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

public String getProfilePic() {
    return profilePic;
}

public void setProfilePic(String profilePic) {
    this.profilePic = profilePic;
}
}

然后在 UserController 类中我有一个 addUser 方法:

@RequestMapping("/api")
@RestController
public class UserController {

private final UserRepository userRepository;

@Autowired
public UserController(UserRepository userRepository) {
    this.userRepository = userRepository;
}


@RequestMapping(method = RequestMethod.POST)
public ResponseEntity addUser(@ModelAttribute User user) {
    userRepository.save(user);

    return new ResponseEntity("User Created!", HttpStatus.OK);
}

@GetMapping
public List<User> getAllUsers() {
    return userRepository.findAll();
}

}

当我以表单数据或表单 urlencoded 形式发送 postman 请求时,它会保存一个新用户,但用户名和个人资料图片始终为空。到目前为止,我已仔细检查拼写超过 15 次,所以我认为不是这样。为什么那 2 总是为空?

postman body

最佳答案

因为在您的 Postman 正文中,您使用 user_nameprofilepic 而不是 usernameprofilePic。还要添加用户名的setter:

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

关于java - 对于 5 个构造函数参数中的 2 个,Postman 对 spring 的 post 调用始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634379/

相关文章:

java - Spring MVC 单元测试 - 我可以将 URL(带参数)传递给 Controller ​​吗?

java - 用于管理具有许多框架且无文档的复杂项目设置的工具

java - Microsoft Project Online SDK API/REST API 支持

.net - 如何使用 Android 使用 WCF 服务

java - 去另一个 Activity 时出错

java - java的用户注册框架

java - 阻塞 'take()' 但有驱逐政策的队列实现

java - 以 arraylist 作为字段的改造帖子

java - 有没有办法在 Java 应用程序运行时获取配置文件?

rest - 从 Paypal 客户端 REST 集成按钮返回客户信息