Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring Web – 405 method not allowed

I recently tried to program a simple api in spring.
When I try it with postman, the only two working endpoints are the fetchAllMovie and the createMovie. The others (with request parameter) give a response:

{
    "timestamp": "2021-11-30T14:38:34.396+00:00",
    "status": 405,
    "error": "Method Not Allowed",
    "path": "/api/movies"
}

Here’s a snippet:

@RestController
@RequestMapping("/api/movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private MovieMapper movieMapper;

    @GetMapping
    public List<Movie> fetchAllMovie() {
        return movieService.getAllMovie();
    }

    @PostMapping
    public MovieDto createMovie(@RequestBody MovieCreationDto movieCreationDto) {
        Movie movie = movieMapper.creationDtoToModel(movieCreationDto);
        return movieMapper.modelToDto(movieRepository.save(movie));
    }

    @GetMapping("/{movieId}")
    public MovieDto fetchMovieById(@PathVariable("movieId") String movieId) throws MovieNotFoundException {
        Movie movie = movieRepository.findById(movieId).orElseThrow(MovieNotFoundException::new);
        return movieMapper.modelToDto(movie);
    }
}

So if I send a GET request like http://localhost:8080/api/movies?movieId=619fa9d9b0c30252474b9a01 I get the error, but if I send a GET or POST request like http://localhost:8080/api/movies i can get all of the data from the data base or I can POST in it. (Of course with the proper request body)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Note it: Not only the GET req not working. Anything with request parameter gives me this error.

>Solution :

The @PathVariable is used to send parameter in path, like this: http://localhost:8080/api/movies/619fa9d9b0c30252474b9a01

If you want to send it using URL you specified, you need to use annotation @RequestParam

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading