I am facing an issue with my Spring Boot application where I have a single controller that handles POST requests only. I have set the context path in my application.yml as server.servlet.context-path: /context/path. The goal is to handle POST requests for both /context/path and /context/path/ URLs.
My controller looks like this:
@RestController
@RequestMapping("")
public class MyController {
@PostMapping({ "", "/" })
public ResponseEntity<String> handlePostRequest() {
// Handling POST request logic
return ResponseEntity.ok("POST request handled");
}
}
When I send a POST request to /context/path, it gets redirected with a 302 status code and the request method changes to GET, and it gets redirected to /context/path/.
I have tried different combinations of @RequestMapping and @PostMapping. Nothing worked.
I found some recommended solutions to create a WebConfiguration and override the configurePathMatch method. But the methods like setUseTrailingSlashMatch or setMatchOptionalTrailingSeparator are deprecated.
Despite these attempts, the issue persists. I tried different sources on the internet but didn't get an answer that worked. I am posing here as a last resort. I know it may not be considered as an issue, but any insights or suggestions on resolving this issue would be greatly appreciated.
Comment From: bclozel
I'm not sure where the redirect comes from. A sample application would help here.
As explained in this issue comment the best way forward is to use a proxy in front of your application or embed a Servlet filter with rewrite rules.
I'm closing this issue for now as it doesn't seem to be a bug in Spring Boot but rather a configuration issue. We can reopen it if it turns out something needs to be done here.
Thanks!
Comment From: sharozmirza
Thank you for your reply. I am good with closing the issue, but I'd like to provide additional information regarding my question. There is no problem when I configure the context path as server.servlet.context-path: /context, and when I update my controller as follows:
@RestController
public class MyController {
@PostMapping({ "/path", "/path/" })
public ResponseEntity<String> handlePostRequest() {
// Handling POST request logic
return ResponseEntity.ok("POST request handled");
}
}
The issue happens when I define the @PostMapping like this: @PostMapping({ "", "/" }) with server.servlet.context-path is set to /context/path.