Following the getting started guide on the Jersey website:
I executed the following build command:
$ mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \ -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \ -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \ -DarchetypeVersion=2.2
Then I followed the tutorial on
https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e6783
add custom ContainerResponseFilter file:
@NameBinding @Retention(RetentionPolicy.RUNTIME) static @interface CORSBinding {} @Provider @Priority(Priorities.HEADER_DECORATOR) @CORSBinding static class CrossDomainFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext creq, ContainerResponseContext cres) { Logger.getLogger("com.example").log( Level.INFO, "before: {0}", cres.getHeaders()); cres.getHeaders().add("Access-Control-Allow-Origin", "*"); cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); cres.getHeaders().add("Access-Control-Allow-Credentials", "true"); cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); cres.getHeaders().add("Access-Control-Max-Age", "1209600"); Logger.getLogger("com.example").log( Level.INFO, "after: {0}", cres.getHeaders()); } } @Provider static class MyResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { System.out.println("MyResponseFilter.postFilter() enter"); responseContext.setEntity( responseContext.getEntity() + ":" + getClass().getSimpleName(), null, MediaType.TEXT_PLAIN_TYPE); System.out.println("MyResponseFilter.postFilter() exit"); } } ... @GET @Produces(MediaType.TEXT_PLAIN) @CORSBinding public String helloWorld() { return "hello world"; }
I tried registering this filter with Named Binding and with Dynamic Binding, nothing works.
To easily reproduce, I also tried an example from official resources:
https://github.com/jersey/jersey/tree/2.2/examples/exception-mapping
Same problem: custom filters are not executed.
Is this a grizzly issue?