gpt4 book ai didi

spring-boot - spring REST RequestMethod如何映射一个http LOCK和UNLOCK RequestMapping?

转载 作者:行者123 更新时间:2023-12-04 15:56:19 24 4
gpt4 key购买 nike

这似乎与Custom HTTP Methods in Spring MVC 相同

我需要使用 http 方法 LOCK 和 UNLOCK 实现调用。

目前spring的requestMethod只支持

public enum RequestMethod {

GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}

如果使用 LOCK 和 UNLOCK http 指令调用 spring 应用程序,我如何实现调用的 @RestController 方法?

最佳答案

您可以使用支持的方法(GET、HEAD、POST、PUT、PATCH、DELETE、OPTIONS、TRACE)调用来完成。下面是内部调用 post 方法的方法:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

@Override
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {

final RequestMappingHandlerAdapter requestMappingHandlerAdapter = super.requestMappingHandlerAdapter();
requestMappingHandlerAdapter.setSupportedMethods(
"LOCK", "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE"
); //here supported method

return requestMappingHandlerAdapter;
}

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

自定义方法处理程序类。这里在内部调用post方法:

public class CustomHttpMethods extends DispatcherServlet {

@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

if ("LOCK".equals(request.getMethod())) {
super.doPost(request, response);
} else {
super.service(request, response);
}
}

}

现在您可以通过以下方式进行请求映射:

@RequestMapping(value = "/custom")
ResponseEntity customHttpMethod(){
return ResponseEntity.ok().build();
}

关于spring-boot - spring REST RequestMethod如何映射一个http LOCK和UNLOCK RequestMapping?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51465855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com