Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest Jersey 2.3拦截器示例不工作_Rest_Jersey_Jersey 2.0 - Fatal编程技术网

Rest Jersey 2.3拦截器示例不工作

Rest Jersey 2.3拦截器示例不工作,rest,jersey,jersey-2.0,Rest,Jersey,Jersey 2.0,我正在尝试使用Tomcat7和名称绑定来实现Jersey 2.3拦截器示例。我已经根据链接中显示的示例创建了以下内容 @NameBinding @保留(RetentionPolicy.RUNTIME) 公共@接口压缩{} 及 压缩 公共类GZIPWriterInterceptor实现WriterInterceptor{ @凌驾 WriteTo(WriterInterceptorContext上下文)周围的public void引发IOException、WebApplicationExcept

我正在尝试使用Tomcat7和名称绑定来实现Jersey 2.3拦截器示例。我已经根据链接中显示的示例创建了以下内容

@NameBinding
@保留(RetentionPolicy.RUNTIME)
公共@接口压缩{}

压缩 公共类GZIPWriterInterceptor实现WriterInterceptor{ @凌驾 WriteTo(WriterInterceptorContext上下文)周围的public void引发IOException、WebApplicationException{ 最终输出流输出流。。。。。 …这里添加了一些日志记录。。 } 及

@Path(“helloword”)
公共类HelloWorldResource{
@得到
@路径(“到多个数据”)
@压缩
公共字符串getVeryLongString(){
String str=“非常。。。
……大串”;
返回str;
}
}
我没有在web.xml中添加任何额外内容来注册拦截器,因为《用户指南》文档中没有讨论这一点。我注意到这是早期版本Jersey中使用的方法

我测试了压缩拦截器是否使用从jersey客户端api构建的客户端工作。我确保了
HttpHeaders.ACCEPT_编码“gzip”
头在发送请求之前添加到
Invocation.Builder
中。我还将日志记录添加到
GZIPWriterInterceptor
。当我测试响应是否压缩时,它会以纯文本返回(
内容类型-text/plain
在响应头中设置)我在日志中没有看到
GZIPWriterInterceptor
消息

有没有人知道我做错了什么,或者我还能尝试什么

编辑-我更新了我的web.xml,试图注册GZIPWriterInterceptor类

。。。。。。
.
Jersey测试Servlet
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
泽西岛
com.ws.rs.ext.WriterInterceptor
com.test.jersey.GZIPWriterInterceptor
1.
.
.......
这不起作用。我是否要用WriterInterceptor的参数名或其他东西注册GZIPWriterInterceptor

更新-我通过扩展javax.ws.rs.core.Application并注册我所需的每个类(HelloWorldResource.class、GZIPWriterInterceptor.class、Compress.class),实现了压缩,如部署无关的应用程序模型中所述

我在web.xml中注册了我的应用程序类

JerseyOrtest2
球衣休息试验
org.glassfish.jersey.servlet.ServletContainer
javax.ws.rs.Application
com.jersey.test.MyApplication
1.
球衣休息试验
/*
我试图通过扩展ResourceConfig(如示例4.2所示)和通过web.xml使用前面的方法注册我的包,但没有成功


jersey.config.server.provider.packages
泽西岛

现在还不清楚为什么软件包注册不起作用。

您需要做两件事:

  • 从提供的代码片段来看,您的
    WriterInterceptor
    似乎没有使用
    @Compress
    注释进行注释。请这样做,以确保提供程序和资源方法匹配在一起
  • 您需要在JAX-RS
    应用程序
    类中或通过
    web.xml
    注册提供程序。文档对此可能有点含糊不清(我们会解决这个问题)

  • 拦截器类也必须用
    @Provider
    注释,如下所示:

    @Provider
    @Compress
    public class GZIPWriterInterceptor implements WriterInterceptor { ...
    
    然后将在包扫描中提取该信息

    文档没有明确地指定它。

    确保所有类(Compress、GZIPWriterInterceptor、HelloWorldResource)都位于web.xml中参数“jersey.config.server.provider.packages”指定的同一个包下

    我突然想到@NameBinding不起作用,我发现我把压缩放在了另一个包中

  • 不要忘记将@Provider注释放在writer类之上
  • 将Writer、Compress和资源放在同一个包中
  • (最重要):在应用程序类上注册类:
  • @ApplicationPath(“myrest”)
    公共类MyRestApplication扩展了应用程序{
    @凌驾
    公共集getSingleton(){
    Set singleton=newhashset();
    添加(新AuthorResource());
    add(新的GZipWriterInterceptor());
    添加(新编码过滤器());
    添加(新的GZipEncoder());
    返回单身人士;
    } 
    }
    
    谢谢Michal,我添加了@Compress并更新了web.xml,以包含我认为注册GZIPWriterInterceptor所需的内容。我会将此作为对我原始问题的编辑。你的参数名是错误的。它应该是
    jersey.config.server.provider.classnames
    ,而不是
    com.ws.rs.ext.WriterInterceptor
    。我知道了我正在使用包注册提供者。