Spring MVC中的自定义HTTP方法 [英] Custom HTTP Methods in Spring MVC

查看:125
本文介绍了Spring MVC中的自定义HTTP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为处理COPY HTTP方法的资源创建自定义Spring MVC控制器。

I am trying to create a custom Spring MVC Controller for a resource that would handle the COPY HTTP method.

@ RequestMapping 只接受以下 RequestMethod 值:GET,HEAD, POST,PUT,PATCH,DELETE,OPTIONS和TRACE。

@RequestMapping accepts only the following RequestMethod values: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS and TRACE.

在Spring MVC控制器中是否有任何推荐的处理自定义HTTP方法的方法?

Is there any recommended way of handling custom HTTP methods in Spring MVC Controller?

推荐答案

Servlet规范仅允许 GET HEAD POST PUT DELETE OPTIONS TRACE HTTP方法。这可以在Apache Tomcat中看到实现Servlet API

The Servlet specification allows only for GET, HEAD, POST, PUT, DELETE, OPTIONS or TRACE HTTP methods. This can be seen in the Apache Tomcat implementation of the Servlet API.

这反映在Spring API的 RequestMethod 枚举

And this is reflected in the Spring API's RequestMethod enumeration.

您可以通过实施自己的 DispatcherServlet 覆盖服务来欺骗自己的方式允许 COPY的方法 HTTP方法 - 将其更改为POST方法,并自定义 RequestMappingHandlerAdapter bean也允许它。

You can cheat your way around those by implementing your own DispatcherServlet overriding the service method to allow COPY HTTP method - changing it to POST method, and customize the RequestMappingHandlerAdapter bean to allow it as well.

像这样的东西,用g spring-boot:

Something like this, using spring-boot:

@Controller
@EnableAutoConfiguration
@Configuration
public class HttpMethods extends WebMvcConfigurationSupport {

    public static class CopyMethodDispatcher extends DispatcherServlet {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
            if ("COPY".equals(request.getMethod())) {
                super.doPost(request, response);
            }
            else {
                super.service(request, response);
            }
        }
    }

    public static void main(final String[] args) throws Exception {
        SpringApplication.run(HttpMethods.class, args);
    }

    @RequestMapping("/method")
    @ResponseBody
    public String customMethod(final HttpServletRequest request) {
        return request.getMethod();
    }

    @Override
    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        final RequestMappingHandlerAdapter requestMappingHandlerAdapter = super.requestMappingHandlerAdapter();
        requestMappingHandlerAdapter.setSupportedMethods("COPY", "POST", "GET"); // add all methods your controllers need to support

        return requestMappingHandlerAdapter;
    }

    @Bean
    DispatcherServlet dispatcherServlet() {
        return new CopyMethodDispatcher();
    }
}

现在你可以调用 / method 使用 COPY HTTP方法的端点。使用 curl 这将是:

Now you can invoke the /method endpoint by using COPY HTTP method. Using curl this would be:

curl -v -X COPY http://localhost:8080/method

这篇关于Spring MVC中的自定义HTTP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆