Postman PUT请求不更新内容,但无错误

wnvonmuf  于 2022-11-07  发布在  Postman
关注(0)|答案(3)|浏览(314)

我正在使用postman和springboot,我已经使用了GET/POST/DELETE请求,它们都工作正常,但是PUT请求不更新内容。
在intellij中,我使用这些文件:
Student.java(with它是setter和getter):

@Entity
@Table
public class Student {
@Id
@SequenceGenerator(
        name="student_sequence",
        sequenceName="student_sequence",
        allocationSize = 1
)
@GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "student_sequence"
)
private Long id;
private String name;

private LocalDate dob;
private  String email;
@Transient
private Integer age;

StudentController.java :

@PutMapping(path ="{studentId}")
public void updateStudent(
        @PathVariable("studentId") Long studentId,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String email)
{
    studentService.updateStudent(studentId,name,email);
}

StudentService.java :

@Transactional
public void updateStudent(Long studentId,String name, String email)
{
   Student student = studentRepository.findById(studentId)
           .orElseThrow(() -> new IllegalStateException(
                   "student with id="+studentId+"does not exist"));
   if (name !=null && name.length()>0  && !Objects.equals(student.getName(),name))
   {
       student.setName(name);
   }
   if (email !=null && email.length()>0 && !Objects.equals(student.getEmail(),email))
   {
       Optional<Student> studentOptional= studentRepository.findStudentByEmail(email);
       if (studentOptional.isPresent())
       {
           throw new IllegalStateException("email taken");
       }
       student.setEmail(email);
   }
 }

These are the students that i have in database基本上,我想更新id=1的学生的姓名和电子邮件。
That is postman header
And that is postman not showing any error after sending request

a0zr77ik

a0zr77ik1#

使用@RequestParam(required = false) String name时,参数应作为标头或查询参数。您正在发送请求正文,因此请改用pojo ...

class StudentDto {

  public String name;
  //...

}

和控制器...

@PutMapping(path ="{studentId}")
public void updateStudent(
        @PathVariable("studentId") Long studentId,
        @RequestBody StudentDto) {
  //...

}
6rqinv9w

6rqinv9w2#

要使它正常工作,您必须将数据作为查询参数,例如

PUT http://localhost:8080/api/v1/student/1?name=newName&email=newEmailToSet
0vvn1miw

0vvn1miw3#

因此,在回答https://stackoverflow.com/a/72698172/19354780后,我尝试将StudentController更改为:

@PutMapping(path = "{studentId}")
public void updateStudent(
        @PathVariable("studentId") Long studentId,
        @RequestBody Student new_student) {
    studentService.updateStudent(studentId, new_student);

}

和学生服务:

@Transactional
 public void updateStudent(Long studentId,Student new_student)
 {
   Student student = studentRepository.findById(studentId)
           .orElseThrow(() -> new IllegalStateException(
                   "student with id="+studentId+"does not exist"));
   if (new_student.getName() !=null && new_student.getName().length()>0  && !Objects.equals(student.getName(),new_student.getName()))
   {
       student.setName(new_student.getName());
   }
   if (new_student.getEmail() !=null && new_student.getEmail().length()>0 && !Objects.equals(student.getEmail(),new_student.getEmail()))
   {
       Optional<Student> studentOptional= studentRepository.findStudentByEmail(new_student.getEmail());
       if (studentOptional.isPresent())
       {
           throw new IllegalStateException("email taken");
       }
       student.setEmail(new_student.getEmail());
   }
   }

而且成功了,但没有例外。

相关问题