Issue
I need to add a custom HTTP Method/Verb to a URI. I need something like this:
@RequestMapping(value = "/device", method = "CAPTURE")
In short, this request needs to work
curl -X CAPTURE http://localhost:4591/device
It is my understanding that @RequestMapping only accepts standardized HTTP Methods, so what options do I have to achieve this.
Solution
You can't do that. You must use standardized HTTP methods. You can create custom endpoints without HTTP method, so, every request will be accepted. (Of course, following some rules, but, isn't the topic of the question I guess)
@RequestMapping(value = "/device")
Or, you can use HTTP headers to allow/block requests. You just need to create a filter in your application. Something like this:
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, CAPTURE");
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
String captureHeader = request.getHeader("CAPTURE");
if (StringUtils.isEmpty(captureHeader)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}else{
chain.doFilter(req, res);
}
}
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
I guess using filter you will be able to do what you trying to do.
Answered By - Cristiano Bombazar