似乎与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 app,如何实现一个@RestController
方法?
答案 0 :(得分:1)
您可以使用支持的方法(GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE)进行此操作。这是内部调用post方法的方法:
@Configuration
public class WebMvcCofig 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();
}