成功 post 请求后的 Postman returns 空值
Postman returns null value after a successful post request
当我在 Postman 上执行 post 请求时,我收到“200 OK”状态。但是做一个 get 请求 returns null JSON values
这是我的用户class
public class User {
private Integer id;
private String name;
private Date birthDate;
public User(Integer id, String name, Date birthDate) {
super();
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return String.format("User [id=%s, name=%s, birthDate=%s]", id, name, birthDate);
}
}
UserDaoService class
@Component
public class UserDaoService {
static List<User> users = new ArrayList<>();
public static int userCount = 3;
static {
users.add(new User(1,"Eva",new Date()));
users.add(new User(2,"Mike",new Date()));
users.add(new User(3,"Dave",new Date()));
}
public List<User> findAllUsers() {
return users;
}
public User save(User user) {
if(user.getId() == null)
user.setId(++userCount);
users.add(user);
return user;
}
public User findOne(int id) {
for(User user:users )
if(user.getId() == id)
return user;
return null;
}
}
和控制器
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping("/users")
public List<User> retrieveAllUsers(){
return service.findAllUsers();
}
@GetMapping("users/{id}")
public User retrieveUser(@PathVariable int id) {
return service.findOne(id);
}
@PostMapping("/users")
public void createUser(User user) {
User saved = service.save(user);
}
}
这是我提出的post请求
{
"name": "Luna",
"birthDate": "2000-08-23T23:58:45.849+00:00"
}
我没有传入 ID,因为它包含在后端中。在发出 get 请求时,name 和 birthDates 都有空值,但 ID 设置正确。
有什么我可能出错的想法吗?
您在 post 方法参数中缺少 @RequestBody
@PostMapping("/users")
public void createUser(@RequestBody User user) {
User saved = service.save(user);
}
当我在 Postman 上执行 post 请求时,我收到“200 OK”状态。但是做一个 get 请求 returns null JSON values
这是我的用户class
public class User {
private Integer id;
private String name;
private Date birthDate;
public User(Integer id, String name, Date birthDate) {
super();
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return String.format("User [id=%s, name=%s, birthDate=%s]", id, name, birthDate);
}
}
UserDaoService class
@Component
public class UserDaoService {
static List<User> users = new ArrayList<>();
public static int userCount = 3;
static {
users.add(new User(1,"Eva",new Date()));
users.add(new User(2,"Mike",new Date()));
users.add(new User(3,"Dave",new Date()));
}
public List<User> findAllUsers() {
return users;
}
public User save(User user) {
if(user.getId() == null)
user.setId(++userCount);
users.add(user);
return user;
}
public User findOne(int id) {
for(User user:users )
if(user.getId() == id)
return user;
return null;
}
}
和控制器
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping("/users")
public List<User> retrieveAllUsers(){
return service.findAllUsers();
}
@GetMapping("users/{id}")
public User retrieveUser(@PathVariable int id) {
return service.findOne(id);
}
@PostMapping("/users")
public void createUser(User user) {
User saved = service.save(user);
}
}
这是我提出的post请求
{
"name": "Luna",
"birthDate": "2000-08-23T23:58:45.849+00:00"
}
我没有传入 ID,因为它包含在后端中。在发出 get 请求时,name 和 birthDates 都有空值,但 ID 设置正确。
有什么我可能出错的想法吗?
您在 post 方法参数中缺少 @RequestBody
@PostMapping("/users")
public void createUser(@RequestBody User user) {
User saved = service.save(user);
}