【问题标题】:HTTP 405 Request method 'PUT' not supported不支持 HTTP 405 请求方法“PUT”
【发布时间】:2023-05-17 15:01:02
【问题描述】:

我试图用 spring boot 做一个 crud,我在邮递员中收到了这个更新错误。

网址:http://localhost:8085/api/v1/student?18&name=student1&email=student@gmail.com

错误类型: "时间戳": "2021-05-15T04:25:55.895+00:00", “状态”:405, "error": "不允许的方法", "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);
}

【问题讨论】:

    标签: java spring spring-boot postman put


    【解决方案1】:

    在我看来,您使用的 URL 是错误的。应该是http://localhost:8085/api/v1/student/18?name=student1studenId 是 PathVariable,因此应该是路径的一部分。您将 18 添加到没有标识符的参数列表中。 此外,您可能需要 URL 编码或 URL 中的@

    “消息”:“不支持请求方法'PUT'”,“路径”:“/api/v1/student” 告诉您它正在寻找路径 /api/v1/student 的 PUT 定义,但没有。在/api/v1/student/18 只有一个

    【讨论】:

    • 是的,你是对的,我现在使用 api/v1/student/18,它可以工作,但是我如何编码我的 url,路径变量的第二部分会产生错误
    • 将@替换为 %40