【发布时间】:2018-09-17 10:56:47
【问题描述】:
我有这门课:
@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);
}
}
由于某种原因,它没有从正文中获取“名称”值,因此它返回了该错误。我需要字段“名称”是不是自动生成的 id(字符串)。
【问题讨论】:
-
为您的实体添加 getter 和 setter。或者使用 JsonCreator 注释的构造函数及其使用 JsonProperty 注释的参数。
-
请勿截图。
标签: java spring rest api spring-boot