Java 对于5个构造函数参数中的2个,Postman对spring的post调用始终为null
我事先道歉,因为我不知道该如何表达这个问题。我有以下课程:Java 对于5个构造函数参数中的2个,Postman对spring的post调用始终为null,java,spring,rest,spring-boot,postman,Java,Spring,Rest,Spring Boot,Postman,我事先道歉,因为我不知道该如何表达这个问题。我有以下课程: @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
@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();
}
}
@RequestMapping(“/api”)
@RestController
公共类用户控制器{
私有最终用户存储库用户存储库;
@自动连线
公共用户控制器(用户存储库用户存储库){
this.userRepository=userRepository;
}
@RequestMapping(method=RequestMethod.POST)
公共响应属性addUser(@modeldattribute User){
userRepository.save(用户);
返回新的响应属性(“用户创建!”,HttpStatus.OK);
}
@GetMapping
公共列表getAllUsers(){
返回userRepository.findAll();
}
}
当我以表单数据或表单URL编码的形式发送邮递员请求时,它会保存一个新用户,但username和profilepic始终为空。到现在为止,我已经反复检查了15次拼写,所以我认为这不是问题所在。为什么这2个总是空的
因为在你的
邮递员
身上你使用了用户名
和档案图片
,而不是用户名
和档案图片
。还为用户名添加setter
:
public void setUsername(String username) {
this.username = username;
}
因为在你的
Postman
身上,你使用user\u name
和profilepic
而不是username
和profilepic
。还为用户名添加setter
:
public void setUsername(String username) {
this.username = username;
}
调试应用程序。别以为有人能猜到。或者至少张贴您发送的
邮递员
正文乍一看,我发现您没有用户名
用户名
的setter可能为空,因为在用户
用户名类中没有setter您缺少setter,对于另一个属性,它是profilePic而不是profilePic。您可能需要研究ObjectMapper
如何处理反序列化。调试应用程序。别以为有人能猜到。或者至少张贴您发送的邮递员
正文乍一看,我发现您没有用户名
用户名
的setter可能为空,因为在用户
用户名类中没有setter您缺少setter,对于另一个属性,它是profilePic而不是profilePic。您可能想研究一下ObjectMapper
如何处理反序列化。我在postman和代码中都将用户名更改为user_name,以查看它是否有效,但在截图oops之前忘了将其更改回去。将profilepic更改为profilepic似乎解决了整个问题,谢谢!如果是这样,请使用复选框接受答案,以便其他人更容易找到答案,如果需要的话。我说我必须等待2分钟。我在邮递员和代码中将用户名更改为用户名,以查看它是否有效,但在截图之前忘记将其更改回去。将profilepic更改为profilepic似乎解决了整个问题,谢谢!如果是这样的话,请使用复选框接受答案,以便其他人更容易找到答案,如果需要的话。我要去,我说我必须等2分钟