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

vddsk6oq  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(183)

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

代码

inMemoryStudentServiceImpl.java

package com.justdeax.coursepractice.Student;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@AllArgsConstructor
public class inMemoryStudentServiceImpl implements StudentService {
    private final StudentDao repo;

    @Override
    public List<Student> findAll() {
        return repo.findAll();
    }

    @Override
    public Student saveStudent(Student student) {
        return repo.saveStudent(student);
    }

    @Override
    public Student findByEmail(String email) {
        return repo.findByEmail(email);
    }

    @Override
    public Student updateStudent(Student student) {
        return repo.updateStudent(student);
    }

    @Override
    public void deleteStudent(String email) {
        repo.deleteStudent(email);
    }
}

字符串

StudentController.java

package com.justdeax.coursepractice.Student;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/students")
@RequiredArgsConstructor
public class StudentController {
    @NonNull
    private final StudentService services;

    @GetMapping
    public List<Student> findAll() {
        return services.findAll();
    }

    @PostMapping("save_student")
    public Student saveStudent(Student student) {
        return services.saveStudent(student);
    }

    @GetMapping("/{email}")
    public Student findByEmail(@PathVariable String email) {
        return services.findByEmail(email);
    }

    @PutMapping("update_student")
    public Student updateStudent(Student student) {
        return services.updateStudent(student);
    }

    @DeleteMapping("delete_student/{email}")
    public void deleteStudent(@PathVariable String email) {
        services.deleteStudent(email);
    }
}

StudentService.java

package com.justdeax.coursepractice.Student;  
import java.util.List;  
public interface StudentService {  
    List<Student> findAll();  
    Student saveStudent(Student student);  
    Student findByEmail(String email);  
    Student updateStudent(Student student);  
    void deleteStudent(String email);  
}

Student.java

package com.justdeax.coursepractice.Student;  
import lombok.*;  
import java.time.LocalDate;  
@Data  
@Builder  
public class Student {  
    private String firstName;  
    private String lastName;  
    private Integer age;  
    private LocalDate dataOfBirth;  
    private String email;  
}

StudentDao.java

package com.justdeax.coursepractice.Student;  
import org.springframework.stereotype.Repository;  
  
import java.util.ArrayList;  
import java.util.List;  
import java.util.stream.IntStream;  
  
@Repository  
public class StudentDao {  
    private final List<Student> STUDENTS = new ArrayList<>();  
    public List<Student> findAll() {  
        return STUDENTS;  
    }  
  
    public Student saveStudent(Student student) {  
        STUDENTS.add(student);  
        return student;  
    }  
  
    public Student findByEmail(String email) {  
        return STUDENTS.stream()  
                .filter(e -> e.getEmail().equals(email))  
                .findFirst()  
                .orElse(null);  
    }  
  
    public Student updateStudent(Student student) {  
        var studentIndex = IntStream.range(0, STUDENTS.size())  
                .filter(i -> STUDENTS.get(i).getEmail().equals(student.getEmail()))  
                .findFirst()  
                .orElse(-1);  
        if (studentIndex > -1) {  
            STUDENTS.set(studentIndex, student);  
            return student;  
        }  
        return null;  
    }  
  
    public void deleteStudent(String email) {  
        var student = findByEmail(email);  
        if (student != null) STUDENTS.remove(student);  
    }  
}

使用次数:

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

你的名字:

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

{
    "firstName": "abc",
    "lastName": "def",
    "age": "99",
    "dataOfBirth": "1000-10-10",
    "email": "[email protected]"
}


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

{
    "firstName": null,
    "lastName": null,
    "age": null,
    "dataOfBirth": null,
    "email": null
}

1bqhqjot

1bqhqjot1#

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

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

字符串

相关问题