使用 Spring REST API [Spring MVC]
设想:
当请求到达时EmployeeController
,如果它属于特定逻辑,则强制将请求/响应转发到另一个 URI。控制器方法已RequestMapping
设置为“ RequestMethod.POST
”,并且目标控制器 -SpecialController
具有 已设置为“ ”的method
名称invalidRequest()
RequestMapping
RequestMethod.GET
员工控制器:
@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;
目标控制器:
@RestController
@RequestMapping(value = "/invalidRequest")
public class SpecialController {
@RequestMapping(value = "", method = RequestMethod.GET)
public String invalidRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
httpResponse.setStatus(401);
return "invalidRequest";
}
}
问题: 不一致问题[实际问题]:
在 90% 的情况下,这是有效的,但很少有几次,我得到以下错误。如果我总是收到这个错误,那么它会有意义,我会有下面提到的“可能的修复”但是因为它在大多数时候都在工作,而只是有时不工作,我需要你的帮助来找出原因?
org.springframework.web.HttpRequestMethodNotSupportedException:在 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping 的 org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:198) 不支持请求方法“POST” .lookupHandlerMethod(AbstractHandlerMethodMapping.java:286) 在 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:233) 在 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56) ) 在 org.springframework.web.servlet.DispatcherServlet 的 org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:300)。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) 在 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)doPost(FrameworkServlet.java:863)doPost(FrameworkServlet.java:863)
如果错误一致,可能的修复:
@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);
}
}