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

时间:2020-01-27 06:32:16

标签: java rest spring-boot tomcat

我有以下控制器

@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 request with "regionId"

第二个“ PUT”请求:给出错误PUT request with "regionId"

数据库表屏幕截图(如果需要) region table

错误日志

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

我以前已经实现了一百次这样的更新方法。但是从来没有遇到这个错误。 我还在 stackoverflow 中找到了类似的错误帖子,例如此链接HTTP Status 405 - Request method 'PUT' not supported 并执行了所有建议,但我的问题没有解决。

1 个答案:

答案 0 :(得分:0)

尝试更改

@PutMapping(path = "/(regionId}")

@PutMapping(path = "/{regionId}")