HTTP 405 Request method 'PUT' not supported

Viewed 1081

Im trying to do a crud with spring boot and I got this error of update in postman.

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

type of error: "timestamp": "2021-05-15T04:25:55.895+00:00", "status": 405, "error": "Method Not Allowed", "message": "Request method 'PUT' not supported", "path": "/api/v1/student"

this is my 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);
        }

    }

}

My controller


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);
    }
}

this is 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));

        };
    }
}

this is my Repository


import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {
Optional <Student> findStudentByEmail(String email);
}
1 Answers

It looks to me like the URL you're using is wrong. It should be http://localhost:8085/api/v1/student/18?name=student1. The studenId is a PathVariable, so should be part of the path. You added the 18 to the parameter list without an identifier. Also you probably need to URL encode or the @ in the URL.

"message": "Request method 'PUT' not supported", "path": "/api/v1/student" tells you that it's looking for a PUT definition at the path /api/v1/student and there is none. There is only one at /api/v1/student/18.

Related