REST - HTTP 状态 405;方法不允许;请求方法“PUT”不支持错误

Posted

技术标签:

【中文标题】REST - HTTP 状态 405;方法不允许;请求方法“PUT”不支持错误【英文标题】:REST - HTTP Status 405; Method Not Allowed; Request method 'PUT' not supported error 【发布时间】:2020-05-12 12:43:56 【问题描述】:

我有以下控制器

@RestController
@RequestMapping("regions")
public class RegionController 

    @Autowired
    RegionService regionService;

    ModelMapper modelMapper = new ModelMapper();

    @PostMapping
    public RegionRest createRegion(@RequestBody RegionDetailsRequestModel regionDetails) 

        RegionDto regionDto = modelMapper.map(regionDetails, RegionDto.class);

        RegionDto createdRegion = regionService.createRegion(regionDto);

        RegionRest returnValue = modelMapper.map(createdRegion, RegionRest.class);
        return returnValue;
    

    @GetMapping(path = "/regionId")
    public RegionRest getRegion(@PathVariable String regionId) 

        RegionDto regionDto = regionService.getRegionByRegionId(regionId);

        RegionRest returnValue = modelMapper.map(regionDto, RegionRest.class);
        return returnValue;
    

    @PutMapping(path = "/(regionId")
    public RegionRest updateRegion(@PathVariable String regionId, @RequestBody RegionDetailsRequestModel regionDetails) 

        RegionDto regionDto = modelMapper.map(regionDetails, RegionDto.class);

        RegionDto updatedRegion = regionService.updateRegion(regionId, regionDto);

        RegionRest returnValue = modelMapper.map(updatedRegion, RegionRest.class);
        return returnValue;
    

这是邮递员打来的我的请求电话。 首先,“GET”请求:工作正常

第二个,“PUT”请求:给出错误

数据库表截图(如果需要)

错误日志

2020-01-27 12:12:22.007  WARN 19860 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-01-27 12:12:22.236  INFO 19860 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-27 12:12:22.252 DEBUG 19860 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2020-01-27 12:12:22.319 DEBUG 19860 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 18 mappings in 'requestMappingHandlerMapping'
2020-01-27 12:12:22.361 DEBUG 19860 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2020-01-27 12:12:22.377 DEBUG 19860 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2020-01-27 12:12:22.517  INFO 19860 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-27 12:12:22.517  INFO 19860 --- [           main] com.azad.practice.hr.HrApplication       : Started HrApplication in 14.398 seconds (JVM running for 15.423)
2020-01-27 12:12:43.189  INFO 19860 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-27 12:12:43.189  INFO 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-27 12:12:43.190 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2020-01-27 12:12:43.202 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2020-01-27 12:12:43.202  INFO 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms
2020-01-27 12:12:43.215 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : PUT "/regions/reg_MCNXENXzF4", parameters=
2020-01-27 12:12:43.222  WARN 19860 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported]
2020-01-27 12:12:43.223 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED
2020-01-27 12:12:43.234 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for PUT "/error", parameters=
2020-01-27 12:12:43.237 DEBUG 19860 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2020-01-27 12:12:43.298 DEBUG 19860 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-01-27 12:12:43.299 DEBUG 19860 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [timestamp=Mon Jan 27 12:12:43 BDT 2020, status=405, error=Method Not Allowed, message=Request metho (truncated)...]
2020-01-27 12:12:43.383  INFO 19860 --- [nio-8080-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics 
    0 nanoseconds spent acquiring 0 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    0 nanoseconds spent preparing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

2020-01-27 12:12:43.384 DEBUG 19860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 405

我之前已经实施过这样的更新方法一百次了。但从未遇到过这个错误。 我还在 *** 中发现了类似的错误帖子,例如此链接 HTTP Status 405 - Request method 'PUT' not supported 并实施了每一个建议,但我的问题没有解决。

【问题讨论】:

首先想到的是:您在代码中输入了一个错字:“/(regionId”) 您使用 ( 并且您关闭 。将左括号更改为正确的类型跨度> 是的,这就是错误!它工作正常。谢谢。对不起我的视力不好:) 我要补充几点:ModelMapper 很慢并且(根据我的经验)使用起来很痛苦; MapStruct 和竞争对手要好得多。使用构造函数注入而不是字段注入。单一 JSON 模型具有不同的“Rest”/“DTO”/“Model”模型似乎要求不同步。 哦,为了将来参考,将 org.springframework.web 的日志级别设置为 DEBUG,您将在启动时获得所有映射的列表,这对于了解 Spring 如何正在解释您的指示。 我同意。我不知道 MapStruct;将来会尝试。至于日志记录:已经这样做了。谢谢。 【参考方案1】:

尝试改变

@PutMapping(path = "/(regionId")

@PutMapping(path = "/regionId")

【讨论】:

他已经这样做了,并且已经说过这确实解决了问题。

以上是关于REST - HTTP 状态 405;方法不允许;请求方法“PUT”不支持错误的主要内容,如果未能解决你的问题,请参考以下文章

Rest API - 不允许使用 405 方法 [重复]

如何使用环回 4 进行修复(将 rest 转换为 graphql):OPTIONS http://localhost:3001/graphql 405(不允许的方法)

不允许通过 REST api 和 jquery 405 方法创建 Jira 问题

WebApi Post 方法总是返回“请求的资源不支持 http 方法 'GET'。”状态:405 方法不允许

WCF 请求失败,HTTP 状态为 405:不允许方法

Spring Boot POST 请求返回 http 状态 405“方法不允许”而不是 HTTP 状态 404