不一致的错误:请求方法' POST'不支持

时间:2015-06-16 18:05:41

标签: java spring spring-mvc servlets spring-restcontroller

使用Spring REST API [Spring MVC]

情境:

当请求到达EmployeeController时,如果请求/响应属于特定逻辑,则会强制将请求/响应转发给另一个URI。 控制器方法RequestMapping设置为' RequestMethod.POST'目标控制器 - SpecialController的{​​{1}}名为methodinvalidRequest()设置为&{39; RequestMapping'

EmployeeController:

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;

问题: 不一致问题[实际问题]:

在90%的情况下,这是有效的,但很少次,我得到以下错误。 如果我总是得到这个错误,那么它会产生一些意义,我会得到下面提到的' 可能的修复' 但由于它大部分时间都在工作,而且有时不工作,我需要你的帮助才能找到原因?

  

org.springframework.web.HttpRequestMethodNotSupportedException:   请求方法' POST'不支持           在org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:198)           在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.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.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:300)           在org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1101)           在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:916)           在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)           在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)           在org.springframework.web.servlet.FrameworkServlet.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";
        }

    }

0 个答案:

没有答案