Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTeasy and X-HTTP-Method-Override

Tags:

java

resteasy

Are there any way that support the X-HTTP-Method-Override request header (automatically/transparently) in RESTeasy?

This would make it much easier to support clients that cannot send PUT/DELETE requests.

Yes, overriding POST is less than ideal but I think the Google convention of using X-HTTP-Method-Override is a reasonable/convenient work-around.

If RESTeasy could dispatch POST requests with the X-HTTP-Method-Override header automatically it would be a big time saver. I think Jersey just added something like this via a filtering approach, but I'd prefer to stick with RESTeasy.

like image 424
A_J Avatar asked Feb 21 '23 15:02

A_J


1 Answers

Recently I had the same problem and the best solution I've found is:

@Provider
@PreMatching
public class OverrideHttpMethodFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext requestContext)
        throws IOException {        
    String receivedMethod = requestContext.getMethod();
    String methodFromHeader = requestContext.getHeaderString("X-HTTP-Method-Override");
    if (receivedMethod != null && !receivedMethod.equals(methodFromHeader)) {
        requestContext.setMethod(methodFromHeader);
    }
  }
}
like image 93
rand0m86 Avatar answered Mar 07 '23 21:03

rand0m86