Using Spring REST API [Spring MVC]
Scenario:
when request comes to the EmployeeController
, it is forced to forward the request/response to another URI,if it falls under a specific logic.
The controller method has RequestMapping
set with 'RequestMethod.POST
' and the destination controller- SpecialController
has the method
named invalidRequest()
that has RequestMapping
set with 'RequestMethod.GET
'
EmployeeController:
@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {
String res = null;
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateEmployeeDetails(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
@Valid @RequestBody Employee emp) throws ServletException, IOException {
// based on logic, forward the request to a different controller that handles invalid request
if( ...) { // condition checking
RequestDispatcher requestDispatcher = httpRequest.getServletContext().getRequestDispatcher("/invalidRequest");
requestDispatcher.forward(httpRequest, httpResponse);
}
if(..someother condition..) {
String res = "something";
}
return res;
Destination Controller:
@RestController
@RequestMapping(value = "/invalidRequest")
public class SpecialController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String invalidRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
httpResponse.setStatus(401);
return "invalidRequest";
}
}
Question: Inconsistency Problem [Actual Problem]:
In 90% of the times, this is working, but a few times very rarely, I get the below error. If I am getting this error always, then it would have made some meaning and I would have the below mentioned 'possible fix' But since it is working most of the times, and not working only sometimes, I need your help in finding out why?
> org.springframework.web.HttpRequestMethodNotSupportedException:
> Request method 'POST' not supported
> at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:198)
> at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:286)
> at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:233)
> at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56)
> at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:300)
> at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1101)
> at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:916)
> at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
> at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
> at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
possible fix if the error was consistent:
@RestController
@RequestMapping(value = "/invalidRequest")
public class SpecialController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String invalidRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
httpResponse.setStatus(401);
return "invalidRequest";
}
@RequestMapping(value = "", method = RequestMethod.POST)
public String invalidRequest2(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
return invalidRequest(httpRequest, httpResponse);
}
}