How to apply interceptors and filters to restful services on Weblogic 12c

徘徊边缘 提交于 2019-12-11 02:48:26

问题


I understand that Weblogic 12c v12.2.1 uses Jersey as a JAX-RS implementation. So I followed the instructions on this page but I haven't succeeded to declare an interceptor whether by using name binding or dynamic binding (i.e. more info in the mentioned link)

My application is working normally because I can actually call the restful services, but I can't apply filters or interceptors, they are never involved in the process.

I didn't edit web.xml at all, all I have is an javax.ws.rs.core.Application child class

@ApplicationPath("rs")
public class MyApp extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> empty = new HashSet<Class<?>>();

    public MyApp() {
        singletons.add(new MyService());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return empty;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

MyService class looks like this

@Path("")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class MyService {
    private static final Logger log = Logger.getLogger(MyService.class);

    @GET
    @Path("login")
    public Status login(@QueryParam(USERNAME_PARAM) String username, @QueryParam(PASSWORD_PARAM) String password, @Context HttpServletRequest request) {
        return new Status(ServiceMessages.USER_AUTHENTICATION_SUCCESS);
    }

I have an empty @Path value because I can't exclude it, I've already specified my path in MyApp class and I don't wan't to specify a path to this class.

My binding class

import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;

import com.mycompany.ws.filters.GZIPCompressor;
public class GzipDynamicBinder implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        context.register(GZIPCompressor.class);
    }

}

My interceptor class

import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;

public class GZIPCompressor implements WriterInterceptor, ReaderInterceptor {

    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
        System.out.println(">>> Compression Reader <<<");
        context.setInputStream(new GZIPInputStream(context.getInputStream()));
        return context.proceed();
    }

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
        System.out.println(">>> Compressor Writer <<<");
        context.setOutputStream(new GZIPOutputStream(context.getOutputStream()));
        context.proceed();
    }

}

I appreciate all answers, but I'd really like an answer that has nothing to do with the web.xml file.


回答1:


  1. Weblogic JAX-RS version is 1.1 (I can't find the concept of Filters or Interceptors anywhere)
  2. Weblogic Jersey version is 1.17, and I was referencing the documentations for the latest version, which is 2.6 at this time.



回答2:


I think what you are missing here is the @Provider annotation in your GZIPCompressor class. Adding that should ensure that the interceptor is called. Check out this blogpost for detailed steps on applying interceptors - http://stick2code.blogspot.in/2015/02/performing-gzip-compression-in-jaxrs-20.html



来源:https://stackoverflow.com/questions/22333128/how-to-apply-interceptors-and-filters-to-restful-services-on-weblogic-12c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!