spring 当我调用post方法填充json时,我得到相同的结果,但在所有字段中为null

vddsk6oq  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(259)

我已经很长时间没有做任何编程相关的事情了(5天),现在我很愚蠢

代码

inMemoryStudentServiceImpl.java

  1. package com.justdeax.coursepractice.Student;
  2. import lombok.AllArgsConstructor;
  3. import org.springframework.stereotype.Service;
  4. import java.util.List;
  5. @Service
  6. @AllArgsConstructor
  7. public class inMemoryStudentServiceImpl implements StudentService {
  8. private final StudentDao repo;
  9. @Override
  10. public List<Student> findAll() {
  11. return repo.findAll();
  12. }
  13. @Override
  14. public Student saveStudent(Student student) {
  15. return repo.saveStudent(student);
  16. }
  17. @Override
  18. public Student findByEmail(String email) {
  19. return repo.findByEmail(email);
  20. }
  21. @Override
  22. public Student updateStudent(Student student) {
  23. return repo.updateStudent(student);
  24. }
  25. @Override
  26. public void deleteStudent(String email) {
  27. repo.deleteStudent(email);
  28. }
  29. }

字符串

StudentController.java

  1. package com.justdeax.coursepractice.Student;
  2. import lombok.NonNull;
  3. import lombok.RequiredArgsConstructor;
  4. import org.springframework.web.bind.annotation.*;
  5. import java.util.List;
  6. @RestController
  7. @RequestMapping("/api/v1/students")
  8. @RequiredArgsConstructor
  9. public class StudentController {
  10. @NonNull
  11. private final StudentService services;
  12. @GetMapping
  13. public List<Student> findAll() {
  14. return services.findAll();
  15. }
  16. @PostMapping("save_student")
  17. public Student saveStudent(Student student) {
  18. return services.saveStudent(student);
  19. }
  20. @GetMapping("/{email}")
  21. public Student findByEmail(@PathVariable String email) {
  22. return services.findByEmail(email);
  23. }
  24. @PutMapping("update_student")
  25. public Student updateStudent(Student student) {
  26. return services.updateStudent(student);
  27. }
  28. @DeleteMapping("delete_student/{email}")
  29. public void deleteStudent(@PathVariable String email) {
  30. services.deleteStudent(email);
  31. }
  32. }

StudentService.java

  1. package com.justdeax.coursepractice.Student;
  2. import java.util.List;
  3. public interface StudentService {
  4. List<Student> findAll();
  5. Student saveStudent(Student student);
  6. Student findByEmail(String email);
  7. Student updateStudent(Student student);
  8. void deleteStudent(String email);
  9. }

Student.java

  1. package com.justdeax.coursepractice.Student;
  2. import lombok.*;
  3. import java.time.LocalDate;
  4. @Data
  5. @Builder
  6. public class Student {
  7. private String firstName;
  8. private String lastName;
  9. private Integer age;
  10. private LocalDate dataOfBirth;
  11. private String email;
  12. }

StudentDao.java

  1. package com.justdeax.coursepractice.Student;
  2. import org.springframework.stereotype.Repository;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.IntStream;
  6. @Repository
  7. public class StudentDao {
  8. private final List<Student> STUDENTS = new ArrayList<>();
  9. public List<Student> findAll() {
  10. return STUDENTS;
  11. }
  12. public Student saveStudent(Student student) {
  13. STUDENTS.add(student);
  14. return student;
  15. }
  16. public Student findByEmail(String email) {
  17. return STUDENTS.stream()
  18. .filter(e -> e.getEmail().equals(email))
  19. .findFirst()
  20. .orElse(null);
  21. }
  22. public Student updateStudent(Student student) {
  23. var studentIndex = IntStream.range(0, STUDENTS.size())
  24. .filter(i -> STUDENTS.get(i).getEmail().equals(student.getEmail()))
  25. .findFirst()
  26. .orElse(-1);
  27. if (studentIndex > -1) {
  28. STUDENTS.set(studentIndex, student);
  29. return student;
  30. }
  31. return null;
  32. }
  33. public void deleteStudent(String email) {
  34. var student = findByEmail(email);
  35. if (student != null) STUDENTS.remove(student);
  36. }
  37. }

使用次数:

  • Lombok岛
  • Sping Boot Starter Web
  • Spring Boot 起动器测试

你的名字:

POST请求时:localhost:8080/API/v1/students/保存_student这个json是这样写的:

  1. {
  2.     "firstName": "abc",
  3.     "lastName": "def",
  4.     "age": "99",
  5.     "dataOfBirth": "1000-10-10",
  6.     "email": "[email protected]"
  7. }


在这个请求之后,响应输出为相同的json,但到处都是null,为什么?

  1. {
  2.     "firstName": null,
  3.     "lastName": null,
  4.     "age": null,
  5.     "dataOfBirth": null,
  6.     "email": null
  7. }

1bqhqjot

1bqhqjot1#

我认为当你收到一个包含JSON数据的POST请求时,你需要在saveStudent方法中注解@RequestBody注解,让Spring知道传入的JSON应该被转换成一个Student对象。

  1. @PostMapping("save_student")
  2. public Student saveStudent(@RequestBody Student student) {
  3. return services.saveStudent(student);
  4. }

字符串

相关问题