spring 不支持PUT请求|Java Sping Boot

gcmastyq  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(185)

我添加了一个put方法到我的控制器。但当我访问它时,我得到错误405(方法不允许)。在控制台中,错误请求方法'PUT'不支持被打印出来。我有一个其他的控制器,在那里put是工作的。
我会感激一些帮助。

请求控制器

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

日志

[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

第1001章:我的application.properties

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

lhcgjxsq1#

[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}

您正在发送上述日志“/api/v1/mainsite?id=1”中所示的请求参数。
但您在代码中使用的是PathVariable:

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

您应该将URL发送为“/api/v1/mainsite/1”

相关问题