我正在尝试通过在控制器方法中使用spring boot 2.5.3来更新实体。
http://localhost:5000/api/v1/student/1
使用以下有效负载。
{
"name":"abc",
"email":"abc@email.com",
"dob":"2000-06-14"
}
这些值不会更新。当我使用调试器检查它们时,它们会得到空值。这是我的控制器方法。
@PutMapping(path = "/{id}")
public ResponseEntity<?> updateStudent(@PathVariable("id") Long id, @RequestParam(required = false) String name, @RequestParam(required = false) String email) {
Student savedStudent = studentService.updateStudent(id, name, email);
return ResponseEntity.ok(savedStudent);
}
电子邮件和姓名是可选的。
调试器中:name:null,email:null
。为什么它们会得到空值?从控制器传递值的正确方式是什么?
@Transactional
// We are not using any query from the repository because we have the service method with transactional annotation.
public Student updateStudent(Long studentId, String name, String email) {
Student student = studentRepository.findById(studentId).orElseThrow(()->new EntityNotFoundException("Student with id " + studentId + " does not exists."));
if (name!= null && name.length()>0 && !Objects.equals(name,student.getName())){
student.setName(name);
}
if (email!= null && email.length()>0 && !Objects.equals(email,student.getEmail())){
Optional<Student> optionalStudent = studentRepository.findStudentByEmail(email);
if (optionalStudent.isPresent()){
throw new IllegalStateException("Email is already taken");
}
student.setEmail(email);
}
System.out.println(student);
Student savedStudent= studentRepository.save(student);
return savedStudent;
}
发布于 2021-08-25 04:43:11
{
"name":"abc",
"email":"abc@email.com",
"dob":"2000-06-14"
}
这不是请求参数,而是请求正文。您需要创建一个类并使用@RequestBody
注释。
@Data
public class UpdateStudentRequest {
private String id;
private String name;
private String email;
}
@PutMapping(path = "/{id}")
public ResponseEntity<?> updateStudent(@PathVariable("id") Long id, @RequestBody UpdateStudentRequest request) {
Student savedStudent = studentService.updateStudent(
request.getId(), request.getName(), request.getEmail());
return ResponseEntity.ok(savedStudent);
}
如果您想将请求参数发送为...URL参数:
http://localhost:5000/api/v1/student/1?name=abc&email=abc@email.com
发布于 2021-08-25 04:43:54
您没有将其作为参数发送(在?
之后)。http://localhost:5000/api/v1/student/1?name=John
可以做到这一点。
发布于 2021-08-25 04:51:27
由于您使用内容主体(在本例中为JSON )对HTTP请求进行POST
,因此需要使用@RequestBody
注释来映射主体:
@PutMapping(path = "/{id}")
public ResponseEntity<?> updateStudent(@PathVariable("id") Long id, @RequestBody StudentDTO student) {
Student savedStudent = studentService.updateStudent(
id, student.getName(), student.getEmail());
return ResponseEntity.ok(savedStudent);
}
StudentDTO
将是一个反映输入有效负载的轻量级类型:
public class StudentDTO {
private String name;
private String email;
private String dob;
// setters and getters
}
否则,为了保留您的RestController
签名并使用@RequestParam
规格化字段,您应该发送以下形式的请求:
http://localhost:5000/api/v1/student/1?name=abc&email=abc@email.com&dob=2000-06-14
https://stackoverflow.com/questions/68923101
复制相似问题