Java SpringMVC中的自定义HTTP方法
我正在尝试为一个资源创建一个定制的SpringMVC控制器,该资源将处理复制HTTP方法 仅接受以下值:GET、HEAD、POST、PUT、PATCH、DELETE、OPTIONS和TRACE 在Spring MVC控制器中,是否有任何处理自定义HTTP方法的推荐方法?只允许Java SpringMVC中的自定义HTTP方法,java,spring-mvc,spring-data-rest,Java,Spring Mvc,Spring Data Rest,我正在尝试为一个资源创建一个定制的SpringMVC控制器,该资源将处理复制HTTP方法 仅接受以下值:GET、HEAD、POST、PUT、PATCH、DELETE、OPTIONS和TRACE 在Spring MVC控制器中,是否有任何处理自定义HTTP方法的推荐方法?只允许获取,头,发布,放置,删除,选项或跟踪HTTP方法。这可以在ApacheTomcat中看到 这反映在SpringAPI中 您可以通过实现自己的DispatcherServlet覆盖服务方法来允许COPYHTTP方法,将其更改
获取,头,发布,放置,删除,选项或跟踪HTTP方法。这可以在ApacheTomcat中看到
这反映在SpringAPI中
您可以通过实现自己的DispatcherServlet
覆盖服务
方法来允许COPY
HTTP方法,将其更改为POST方法,并自定义bean以允许它
类似这样,使用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();
}
}
现在,您可以使用COPY
HTTP方法调用/method
端点。使用curl
这将是:
curl -v -X COPY http://localhost:8080/method
这在最新的Spring Boot Starter 2.2.5中不起作用。您可以覆盖Spring Boot 2.2.6中的HTTP方法: