【问题标题】:How to apply interceptors and filters to restful services on Weblogic 12c如何将拦截器和过滤器应用于 Weblogic 12c 上的 RESTful 服务
【发布时间】:2014-03-11 18:11:37
【问题描述】:

我了解 Weblogic 12c v12.2.1 使用 Jersey 作为 JAX-RS 实现。因此,我按照this page 上的说明进行操作,但无论是使用 name binding 还是 dynamic binding 都没有成功声明拦截器(即上述链接中的更多信息)

我的应用程序工作正常,因为我实际上可以调用 RESTful 服务,但我不能应用过滤器或拦截器,它们从不参与该过程。

我根本没有编辑 web.xml,我只有一个 javax.ws.rs.core.Application 子类

@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 类如下所示

@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);
    }

我有一个空的 @Path 值,因为我无法排除它,我已经在 MyApp 类中指定了我的路径,我不想指定此路径类。

我的绑定类

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);
    }

}

我的拦截器类

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();
    }

}

感谢所有答案,但我真的很想要一个与 web.xml 文件无关的答案。

【问题讨论】:

    标签: jersey weblogic jax-rs weblogic12c jersey-2.0


    【解决方案1】:
    1. Weblogic JAX-RS 版本是 1.1(我在任何地方都找不到过滤器或拦截器的概念)
    2. Weblogic Jersey 版本是 1.17,我参考的是最新版本的文档,目前是 2.6。李>

    【讨论】:

    • 这个问题是怎么回答的?
    【解决方案2】:

    我认为您在这里缺少的是 GZIPCompressor 类中的 @Provider 注释。添加应该确保调用拦截器。 查看这篇博文,了解应用拦截器的详细步骤 - http://stick2code.blogspot.in/2015/02/performing-gzip-compression-in-jaxrs-20.html

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2016-11-16
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多