0

I have the following 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;
    }
}

Here is my requests call from Postman. First, "GET" request: works fine. GET request with "regionId"

Second, "PUT" request: giving error. PUT request with "regionId"

Database table screenshot if needed region table

Error log

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

I've previously implemented update method like this a hundred time. But never faced this error. I've also found similar error post in stackoverflow like this link HTTP Status 405 - Request method 'PUT' not supported and implemented every suggestions but my problem is not solved.

  • 3
    first thing that jumps to mind: you made a typo in your code: "/(regionId}") you open with ( and you close with }. Change the opening bracket to the right type – Stultuske Jan 27 '20 at 06:34
  • Yeah, that's the error!! It's working fine. Thanks. And sorry for my weak eyesight :) – Azhar Uddin Azad Jan 27 '20 at 06:40
  • 1
    A few minor notes I'd add: ModelMapper is slow and (in my experience) a pain to use; MapStruct and competitors are much better. Use constructor injection instead of field injection. Having different "Rest"/"DTO"/"Model" models for a unitary JSON model seems like asking to get out of sync. – chrylis -cautiouslyoptimistic- Jan 27 '20 at 07:01
  • 1
    Oh, and for future reference, set the log level for `org.springframework.web` to DEBUG and you'll get a listing of all of the mappings at startup, which can be really helpful in figuring out how Spring is interpreting your instructions. – chrylis -cautiouslyoptimistic- Jan 27 '20 at 07:04
  • I agree. I didn't know about MapStruct; will try it in future. And as for logging: already done that. Thanks. – Azhar Uddin Azad Jan 27 '20 at 09:43

1 Answers1

0

Try changing

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

to

@PutMapping(path = "/{regionId}")
Gaurav Dhiman
  • 953
  • 6
  • 11