Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 2.3 Interceptor Example not working

I'm trying to get the Jersey 2.3 Interceptor example found here using Tomcat 7 and name binding to work. I've created the following as per the example shown in the link...

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface Compress{}

and

@Compress
public class GZIPWriterInterceptor implements WriterInterceptor {
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
  final OuputStream outputStream .....  
  ..some logging added here..
}

and

@Path("helloword")
public class HelloWorldResource {

  @GET
  @Path("to-much-data")
  @Compress
  public String getVeryLongString(){
    String str = "a very ...
    ..... large string";
    return str;
  }
}

I haven't added anything extra to the web.xml to register the Interceptor as this wasn't discussed in the User Guide documentation. I note that this was the method used in ealier version of Jersey.

I tested if the compression Interceptor worked using a client built from the jersey client api. I've ensured the HttpHeaders.ACCEPT_ENCODING, "gzip" Header is added to the Invocation.Builder prior to sending the request. I've also added logging to GZIPWriterInterceptor. When I test to see if the response is zipped it comes back in plain text (Content-Type - text/plain is set in the response header) and I don't see the GZIPWriterInterceptor message in the logs.

Does anyone have any ideas what I'm doing wrong or what else I could try?

EDIT - I updated my web.xml to attempt to register the GZIPWriterInterceptor class.

......
.
<servlet>
  <servlet-name>Jersey Test Servlet</servlet-name>
  <servlet-value>org.glassfish.jersey.servlet.ServletContainer</servlet-value>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.test.jersey</param-value>
  </init-param>
  <init-param>
    <param-name>com.ws.rs.ext.WriterInterceptor</param-name>
    <param-value>com.test.jersey.GZIPWriterInterceptor</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
.
.......

This hasn't worked. Do I register the GZIPWriterInterceptor with the parameter name of WriterInterceptor or something else?

UPDATE - I've managed to get the compression to work by extending javax.ws.rs.core.Application and registering each class I require (HelloWorldResource.class, GZIPWriterInterceptor.class, Compress.class) as discribed in the Deployment Agnostic Application Model found here.

I registered my Application class in the web.xml...

<display-name>JerseyInterceptorTest2</display-name>
  <servlet>
    <servlet-name>Jersey Rest Test</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.jersey.test.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
     <servlet-name>Jersey Rest Test</servlet-name>
     <url-pattern>/*</url-pattern>
  </servlet-mapping>

I tried unsuccessfully to register my packages by extending ResourceConfig as per Example 4.2. and by the earlier method throught the web.xml

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.test.jersey</param-value>
</init-param>

It's still not clear why package registration doesn't work.

like image 680
Jimbo Jambo Avatar asked Dec 11 '22 10:12

Jimbo Jambo


1 Answers

There are two things you need to do:

  1. From provided code snippet it doesn't seem that your WriterInterceptor is annotated with @Compress annotation. Please, do so to make sure your provider and resource method are matched together.
  2. You need to register the provider in your JAX-RS Application class or through web.xml. The documentation might be a little vague about this (we'll fix that).
like image 98
Michal Gajdos Avatar answered Jan 12 '23 08:01

Michal Gajdos