gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:41:22 31 4
gpt4 key购买 nike

我有以下 Controller

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

这是我从 postman 打来的请求。首先,“GET”请求:工作正常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并实现了所有建议,但我的问题没有解决。

最佳答案

尝试改变

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

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

关于java - REST - HTTP 状态 405;不允许的方法;请求方法 'PUT'不支持错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926084/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com