不支持 HTTP 405 请求方法 'PUT'
HTTP 405 Request method 'PUT' not supported
我正在尝试使用 spring 引导进行处理,但我在邮递员中遇到了这个更新错误。
URL:http://localhost:8085/api/v1/student?18&name=student1&email=student@gmail.com
错误类型:
“时间戳”:“2021-05-15T04:25:55.895+00:00”,
“状态”:405,
“错误”:“不允许的方法”,
"message": "不支持请求方法 'PUT'",
“路径”:“/api/v1/student”
这是我的studentService.java
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.transaction.Transactional;
import java.lang.IllegalStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> GetStudents() {
return this.studentRepository.findAll();
}
public void addNewStudent(Student student) {
Optional<Student> studentOptional = studentRepository.findStudentByEmail(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
studentRepository.save(student);
}
public void deleteStudentByID(Long studentID) {
boolean exists = studentRepository.existsById(studentID);
if (!exists) {
throw new IllegalStateException("student with " + studentID + " does not exist!");
}
studentRepository.deleteById(studentID);
}
@Transactional
public void updateStudentByID(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(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
student.setEmail(email);
}
}
}
我的控制器
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> GetStudents() {
return studentService.GetStudents();
}
@PostMapping
public void registerNewStudent(@RequestBody Student student) {
studentService.addNewStudent(student);
}
@DeleteMapping(path = "{studentId}")
public void deleteStudent(@PathVariable("studentId") Long studentID) {
studentService.deleteStudentByID(studentID);
}
@PutMapping(path = "{studentId}")
public void updateStudent(@PathVariable("studentId") Long studentID, @RequestParam(required = false) String name,
@RequestParam(required = false) String email) {
studentService.updateStudentByID(studentID, name, email);
}
}
这是studentConfig.java
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StudentConfig {
@Bean
CommandLineRunner commandLineRunner(StudentRepository repository) {
return args -> {
Student st1= new Student("student11", "email", "17", 21);
Student st2= new Student("student22", "email", "17", 22);
repository.saveAll(List.of(st1, st2));
};
}
}
这是我的存储库
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional <Student> findStudentByEmail(String email);
}
在我看来,您使用的 URL 有误。应该是http://localhost:8085/api/v1/student/18?name=student1
。
studenId
是 PathVariable,因此应该是路径的一部分。您将 18 添加到没有标识符的参数列表中。
此外,您可能需要 URL 编码或 URL.
中的 @
"message": "请求方法 'PUT' 不支持", "path": "/api/v1/student"
告诉您它正在寻找路径 /api/v1/student
中的 PUT 定义,并且有 none。在 /api/v1/student/18
.
只有 个
我正在尝试使用 spring 引导进行处理,但我在邮递员中遇到了这个更新错误。
URL:http://localhost:8085/api/v1/student?18&name=student1&email=student@gmail.com
错误类型: “时间戳”:“2021-05-15T04:25:55.895+00:00”, “状态”:405, “错误”:“不允许的方法”, "message": "不支持请求方法 'PUT'", “路径”:“/api/v1/student”
这是我的studentService.java
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.transaction.Transactional;
import java.lang.IllegalStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> GetStudents() {
return this.studentRepository.findAll();
}
public void addNewStudent(Student student) {
Optional<Student> studentOptional = studentRepository.findStudentByEmail(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
studentRepository.save(student);
}
public void deleteStudentByID(Long studentID) {
boolean exists = studentRepository.existsById(studentID);
if (!exists) {
throw new IllegalStateException("student with " + studentID + " does not exist!");
}
studentRepository.deleteById(studentID);
}
@Transactional
public void updateStudentByID(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(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
student.setEmail(email);
}
}
}
我的控制器
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> GetStudents() {
return studentService.GetStudents();
}
@PostMapping
public void registerNewStudent(@RequestBody Student student) {
studentService.addNewStudent(student);
}
@DeleteMapping(path = "{studentId}")
public void deleteStudent(@PathVariable("studentId") Long studentID) {
studentService.deleteStudentByID(studentID);
}
@PutMapping(path = "{studentId}")
public void updateStudent(@PathVariable("studentId") Long studentID, @RequestParam(required = false) String name,
@RequestParam(required = false) String email) {
studentService.updateStudentByID(studentID, name, email);
}
}
这是studentConfig.java
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StudentConfig {
@Bean
CommandLineRunner commandLineRunner(StudentRepository repository) {
return args -> {
Student st1= new Student("student11", "email", "17", 21);
Student st2= new Student("student22", "email", "17", 22);
repository.saveAll(List.of(st1, st2));
};
}
}
这是我的存储库
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional <Student> findStudentByEmail(String email);
}
在我看来,您使用的 URL 有误。应该是http://localhost:8085/api/v1/student/18?name=student1
。
studenId
是 PathVariable,因此应该是路径的一部分。您将 18 添加到没有标识符的参数列表中。
此外,您可能需要 URL 编码或 URL.
@
"message": "请求方法 'PUT' 不支持", "path": "/api/v1/student"
告诉您它正在寻找路径 /api/v1/student
中的 PUT 定义,并且有 none。在 /api/v1/student/18
.