405 JSP错误与Put方法


问题内容

我遇到了有关JSP不接受PUT请求的问题。所以我想知道如何解决它。我已经在堆栈溢出中阅读了这个相关的问题,但是没有说明如何解决它。

来自Rails的背景,我正在尝试做到这一点,所以我在使用Rails REST样式,例如PUT用于更新,而DELETE用于删除User资源。

但是,只要此控制器出现错误,它就会尝试将请求返回到原始JSP,但是对于Tomcat 8.0.9,它不接受该请求,并显示以下错误:“
HTTP状态405-JSP仅允许GET POST或HEAD”。我尝试在Tomcat web.xml中禁用只读-
没有任何效果,但我仍然收到错误。我已将其切换为POST方法,流程运行正常。

有没有办法在仍然接受请求的PUT方法的同时强制将其转发为POST方法?

/**
     * Edit a user account.
     * @return the edit user view
     */
    @RequestMapping(value = {"/update/{userId}"}, method = RequestMethod.PUT)
    public String updateUser(@Valid @ModelAttribute("user") User user, BindingResult result, final RedirectAttributes redirectAttributes)
    {
        logger.debug(user);

        // we check for duplicate email addresses during the update operation.
        List<User> userCheckList = userRepository.findByEmail(user.getEmail());
        if (userCheckList.size() > 0)
        {
            // size of list should only ever be 1
            User userCheck = userCheckList.get(0);
            if (userCheck.getId() != user.getId())
            {
                result.rejectValue("email", "error.user", "An account already exists for this user email address.");
            }
        }


        if (result.hasErrors())
        {
            return "admin.users.edit";
        }

        // we locate the user and add it to the model
        userRepository.save(user);



        // the save operation was successful so we show the user message.
        redirectAttributes.addFlashAttribute("user", user);
        redirectAttributes.addFlashAttribute("message", "Updated successfully");


        String viewName = "redirect:/admin/users";
        logger.debug(viewName);

        return viewName;
    }

问题答案:

问题是,当您从控制器方法返回视图名称时,Spring DispatcherServlet将转发给定视图,并保留原始PUT方法。

在尝试处理此问题时,Tomcat会拒绝它,理由是PUT可以将对JSP的解释为“用请求的内容替换服务器上的此JSP文件”。

确实,您希望控制器处理您的PUT请求,然后以形式转发给您的JSP GET。幸运的是,Servlet
3.0提供了一种仅在FORWARD调度程序上进行过滤的方法。

创建一个过滤器:

public class GetMethodConvertingFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // do nothing
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

        chain.doFilter(wrapRequest((HttpServletRequest) request), response);
    }

    @Override
    public void destroy() {
        // do nothing
    }

    private static HttpServletRequestWrapper wrapRequest(HttpServletRequest request) {
        return new HttpServletRequestWrapper(request) {
            @Override
            public String getMethod() {
                return "GET";
            }
        };
    }
}

然后将其连接到您的web.xml

<filter>
    <filter-name>getMethodConvertingFilter</filter-name>
    <filter-class>my.GetMethodConvertingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>getMethodConvertingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

这会将请求GET仅转换为正向转发,而使通过其他调度程序的请求保持不变,因此PUTs将被控制器正常拦截。

我的理解(可能是错误的)是Tomcat 8.0.9引入了一个修补程序,该修补程序可以自动为ERROR调度程序自动完成-
请参阅链接问题中的答案。但是您没有使用容器的错误处理机制来呈现错误页面,而是使用Spring
MVC手动转发到视图,因此为什么需要这样做。我个人在Jetty9.2.7下遇到了此问题,该问题没有适当的修复,并且我将错误处理委托给了容器,因此我也在<dispatcher>ERROR</dispatcher>过滤器映射中进行了配置。

这一切似乎有些不可思议,但这是我发现成功跳过这种特定的RESTful-Spring-JSP-web-application-箍的唯一方法。