【发布时间】: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