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
CodePudding user response:
[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"