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

PUT Request not supported | Java Spring boot

I added a put Method to my Controller. But when i access it , i get the error 405 (Method not Allowed). In the Console the error Request method ‘PUT’ not supported is printed.
I have an other controller where the put is working.

I would apriciate some help.

RequestController

@RestController
@RequestMapping("/mainsite")
public class MainsiteController {
    private MainsiteService mainsiteService;

    @Autowired
    public MainsiteController(MainsiteService mainsiteService) {
        this.mainsiteService = mainsiteService;
    }

    @PutMapping("{id}")
    public Mainsite updateMainsite(@PathVariable("id") Long id, @RequestBody Mainsite mainsite) {
        return mainsiteService.updateMainsite(id, mainsite);
    }
}

Log

[2m2022-11-20 17:47:49.587[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m PUT "/api/v1/mainsite?id=1", parameters={masked}
[2m2022-11-20 17:47:49.592[0;39m [33m WARN[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported]
[2m2022-11-20 17:47:49.592[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed 405 METHOD_NOT_ALLOWED
[2m2022-11-20 17:47:49.595[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m "ERROR" dispatch for PUT "/api/v1/error?id=1", parameters={masked}
[2m2022-11-20 17:47:49.596[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
[2m2022-11-20 17:47:49.613[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
[2m2022-11-20 17:47:49.616[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.w.s.m.m.a.HttpEntityMethodProcessor [0;39m [2m:[0;39m Writing [{timestamp=Sun Nov 20 17:47:49 CET 2022, status=405, error=Method Not Allowed, path=/api/v1/mainsite (truncated)...]
[2m2022-11-20 17:47:49.640[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Exiting from "ERROR" dispatch, status 405

application.properties

server.servlet.context-path=/api/v1

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

>Solution :

[2m2022-11-20 17:47:49.587[0;39m [32mDEBUG[0;39m [35m10420[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m PUT "/api/v1/mainsite?id=1", parameters={masked}

You are sending request parameter as shown in above log "/api/v1/mainsite?id=1".

but you are using PathVariable in your code:

   public Mainsite updateMainsite(@PathVariable("id") Long id, @RequestBody Mainsite mainsite) {

You should send the url as "/api/v1/mainsite/1"

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