0

Spring REST API の使用 [Spring MVC]

シナリオ:

にリクエストが来るとEmployeeController、特定のロジックに該当する場合、リクエスト/レスポンスを別の URI に強制的に転送します。コントローラ メソッドはRequestMapping' ' で設定されRequestMethod.POST、宛先コントローラには ' 'で設定されSpecialControllermethod名前invalidRequest()がありますRequestMappingRequestMethod.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: リクエスト メソッド 'POST' は org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:198) でサポートされていません org.springframework.web.servlet.handler.AbstractHandlerMethodMapping でorg.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:233) の .lookupHandlerMethod(AbstractHandlerMethodMapping.java:286) 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.org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:916) の getHandler(DispatcherServlet.java:1101) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) の org.springframework org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) の .web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)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);
        }

    }
4

0 に答える 0