如何将拦截器和筛选器应用于Weblogic 12c上的静态服务 [英] How to apply interceptors and filters to restful services on Weblogic 12c
问题描述
我了解到 Weblogic 12c v12.2.1 使用 Jersey 作为 JAX-RS 实施.因此,我按照此页面上的说明进行操作,但是我没有通过使用名称绑定或动态绑定(即上述链接中的更多信息)来声明拦截器
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.
我根本没有编辑 web.xml ,我只有一个javax.ws.rs.core.Application
子类
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类如下
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);
}
我有一个空的@Path
值,因为我不能排除它,我已经在 MyApp 类中指定了我的路径,而且我也不想为此指定路径课.
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.
我的绑定类
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 文件无关的答案.
I appreciate all answers, but I'd really like an answer that has nothing to do with the web.xml file.
推荐答案
- Weblogic JAX-RS 版本是 1.1 (我在任何地方都找不到过滤器或拦截器的概念)
- Weblogic Jersey 的版本是 1.17 ,目前我正在参考最新版本的文档,该文档的最新版本是 2.6 .
- Weblogic JAX-RS version is 1.1 (I can't find the concept of Filters or Interceptors anywhere)
- Weblogic Jersey version is 1.17, and I was referencing the documentations for the latest version, which is 2.6 at this time.
这篇关于如何将拦截器和筛选器应用于Weblogic 12c上的静态服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!