我有这堂课:
@Entity
@Table(name = "users")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt"}, allowGetters = true)
public class User implements Serializable {
@Id
@NotBlank
private String name;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
}
此存储库:
@Repository
public interface UserRepository extends JpaRepository<User, String> { }
这个控制器:
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
UserRepository userRepository;
// Get All Users
@GetMapping("/users")
public List<User> getAllUsers() {
return userRepository.findAll();
}
// Create a new User
@PostMapping("/users")
public User createUser(@Valid @RequestBody User user) {
return userRepository.save(user);
}
}
由于某种原因,它没有从正文中获取“名称”值,因此它返回该错误。我需要字段“name”作为不自动生成的id(字符串)。
答案 0 :(得分:0)
我似乎是财产&#34;名称&#34;是某种受保护的财产,我把它改成了 &#34;用户名&#34;现在它有效。
答案 1 :(得分:0)
为您的用户实体生成getter和setter:
@Id
@NotBlank
private String name;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
这应解决您的400错误,我复制了您的代码并且它对我有用