问题标签 [jersey-2.0]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - Netbeans 7.4 JAX-RS Servlet 类
我正在尝试在 NetBeans 7.4(今天发布)和 GlassFish 4 服务器下开发 JAX-RS。
显然,NetBeans 7.4 应该使用 Jersey 2.0,但是查看项目中的 library->GlassFish Server 下的库,我只看到几个通用 jar:
- javax.servlet-api.jar
- javax.ws.rs-api.jar
- ...
所以我不确定泽西岛和服务器使用的是哪个版本。所以第一个问题是:我是否需要在运行时使用与编译时相同的 JAX-RS 实现?我怎么知道我在 NetBeans 7.4 下使用的是什么版本?
现在使用 com.sun.jersey.spi.container.servlet.ServletContainer 作为 web.xml 中的 Servlet 类返回
在使用 org.glassfish.jersey.spi.container.servlet.ServletContainer 时
所以第二个问题是:什么是正确的 Servlet 类?
在 WEB-INF/lib 下添加从https://jersey.java.net/download.html下载的 Jersey 2.0库,并使用后者的 Servlet 类产生:
编辑:
从 WEB-INF 中的 lib/ 文件夹中删除库(如 Michal Gajdos 建议的那样)可以避免运行时错误。因此 web.xml 现在是
但是 JAX-RS 仍然不想启动,并且服务器日志上没有显示任何内容。我还缺少其他指令吗?
撞!
unit-testing - 无法模拟 Glassfish Jersey 客户端响应对象
我在创建用于单元测试的模拟响应对象时遇到问题。我正在使用org.glassfish.jersey.core.jersey-client
2.3.1 版来实现我的 RESTful 客户端和mockito
1.9.5 版来帮助我处理模拟对象。这是我的测试代码:
执行时出现问题this.client.getAll();
。这是该方法的代码:
请注意,我正在使用手动创建的响应来模拟 this.simpleClient.getAllWithResponse() 方法。当它到达response.readEntity(resourceListType);
指令时,Jersey 会抛出以下异常:java.lang.IllegalStateException - Method not supported on an outbound message.
. 经过大量的研究和调试后,由于某种原因,当我使用响应构建器创建一个响应时,例如Response.ok(expectedResource, MediaType.APPLICATION_JSON).build();
它将它创建为 OutboundResponse 而不是InboundResponse。后者是唯一允许使用该Response.readEntity()
方法的人。如果是OutboundResponse,则抛出异常。
但是,我找不到将手动创建的响应转换为 InboundResponse 的任何方法。所以我的测试注定要失败:(。你们知道我能在这里做什么吗?我不想用 Mockito 模拟 Response 对象,因为我认为它可能是代码味道,因为它违反了法律得墨忒耳。真的,我在这里没有想法。这样的事情应该简单明了。
jersey - 什么是 HttpRequestContext.getEntity(Class) 的 Jersey 2 替代品?
当资源尝试解析请求实体时,我想记录实体的字符串表示,以便以后重现问题。过去我曾经:
- 在资源读取实体之前缓存它。
- 调用 HttpRequestContext.getEntity(String.class)。
jersey - Jersey 2:如何将参数从 web.xml 传递到应用程序?
我的 Web 容器知道我的应用程序是在调试模式还是在发布模式下运行。我想将此信息传递给我的 ResourceConfig/Application 类,但不清楚如何将这些信息读回。
是否可以通过 servlet/filter 参数传递信息?如果是这样,怎么做?
jersey - 球衣 2:什么取代了 FEATURE_FILTER_FORWARD_ON_404?
Jersey 1 中的ServletContainer.FEATURE_FILTER_FORWARD_ON_404的 Jersey 2 等价物是什么?
ServletContainer.FEATURE_FILTER_FORWARD_ON_404定义为:
如果为 true 并且从运行时或应用程序返回没有实体主体的 404 响应,则运行时将请求转发到过滤器链中的下一个过滤器
请解释你为什么投反对票。如果您不解释问题所在,我将无法改进问题/答案。
rest - Custom MessageBodyReader not found in JerseyTest
I am having a bizarre problem with my JerseyTest class.
When executing my test code and putting a break point on line 203 of org.glassfish.jersey.message.internal.ReaderInterceptorExecutor, I see that my reader is not in reader.workers. However, as you can see below, I register this MessageBodyReader in the ResourceConfig.
All relevant code is provided below.
My custom MessageBodyReader/Writer
And yes, isReadable
returns true.
When debugging, I see that the code hits writeTo
but not readFrom
.
Test code that fails
(Pulling from this example.)
The resource it's testing...
(Note that the web service annotations such as @GET are in the interface.)
Generates this fail trace
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/vnd.com.company-v1+json, type=class com.company.rest.v1.resources.location.json.LocationListV1, genericType=class com.company.rest.v1.resources.location.json.LocationListV1. at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:207) at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:139) at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1109) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785) at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96) at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:761) at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90) at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396) at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296) at com.company.rest.resources.location.TestLocationResource.basicTest(TestLocationResource.java:47) [...]
... with this console output
Followed by this, implying that there was a successful response
Anybody have any idea what I am doing wrong?
rest - Jersey 2.3 拦截器示例不起作用
我正在尝试使用 Tomcat 7 和名称绑定来获取此处找到的 Jersey 2.3 Interceptor 示例。我根据链接中显示的示例创建了以下内容...
和
和
我没有在 web.xml 中添加任何额外的东西来注册拦截器,因为这在用户指南文档中没有讨论。我注意到这是早期版本的泽西岛使用的方法。
我测试了压缩拦截器是否使用从球衣客户端 api 构建的客户端工作。我已确保在发送请求之前将HttpHeaders.ACCEPT_ENCODING, "gzip"
标头添加到前面。Invocation.Builder
我还添加了日志记录到GZIPWriterInterceptor
. 当我测试响应是否被压缩时,它会以纯文本形式返回(Content-Type - text/plain
在响应标头中设置)并且我GZIPWriterInterceptor
在日志中看不到该消息。
有谁知道我做错了什么或者我还能尝试什么?
编辑- 我更新了我的 web.xml 以尝试注册 GZIPWriterInterceptor 类。
这没有奏效。我是否使用 WriterInterceptor 的参数名称或其他名称注册 GZIPWriterInterceptor?
更新- 我已经设法通过扩展 javax.ws.rs.core.Application 并注册我需要的每个类(HelloWorldResource.class、GZIPWriterInterceptor.class、Compress.class)来使压缩工作,如部署不可知应用程序模型中所述在这里找到。
我在 web.xml 中注册了我的应用程序类...
我尝试按照示例 4.2 通过扩展 ResourceConfig 来注册我的包,但没有成功。并通过 web.xml 的早期方法
目前尚不清楚为什么包注册不起作用。
eclipse - 在 Eclipse 插件中注入 Jersey 客户端
我正在开发一个 Eclipse 插件。我正在尝试将 Jersey 客户端(jersey-client-2.3.1)绑定到这个插件。
据我所知,我需要 HK2 进行依赖注入。当我从 Eclipse 或 JUnit 测试运行类时,这很好。
当我想将项目作为“Eclipse Application”运行时,问题就来了。Eclipse 的新实例打开,然后它无法解析/创建 Jersey 客户端的实例。通过 ClientBuilder.newClient() 调用会产生异常,并尝试使用 @Inject 进行注释会产生 null。好像没有上下文。
我应该怎么做才能使它工作?在我运行单元测试的 Eclipse 中,一切正常,因为只有一个(Jersey)lib 提供程序,所以它默认绑定它。如何在 Eclipse 的新实例中做同样的事情?
这是我正在使用的库列表:
maven - 未调用 LoggingFilter - 给出 HTTP 404 - Not Found 错误
我正在使用 Jersey 2.3.1、Tomcat 7.0、maven。
我有一个简单的 jersey servlet:
并想添加一个 LoggingFilter。
这是我的 web.xml:
当我运行 tomcat 服务器时,服务器启动,但是当我尝试访问 URL 时,我收到“HTTP 状态 404 - 未找到”错误。我在过滤器类中有调试断点,所以我可以看到它永远不会被调用。
我尝试了在 web.xml 中配置 loggingFilter 的各种不同方法,即:
在 servlet 标记内部 - 这会运行服务器并显示资源,但不会再次调用过滤器。
java - 在 Osgi 中以编程方式注册 ServletContainer
我正在使用动态加载资源的 OSGI 开发 JAX-RS 应用程序。为此,我需要以编程方式注册 ServletContainer,然后我可以调用方法 ServletContainer.reload (ResourceConfig)。我正在使用 PAX-WEB 3.0.2 和 Jersey 2.4 在支持 servlet 3.x 的 OSGI 环境中运行。问题是当我想访问实现服务器发送事件的资源时,Jersey 会引发一个不支持异步的错误,因为它在 Servlet 2.x 中运行。
这里例外:
我尝试以各种方式注册 ServletContainer,但都没有效果,我认为问题出在 Jersey 的注册和设置中,因为我记录了一个基于服务器发送事件的异步 Servlet,一切都很完美。
代码示例:
这是我的代码存储库。请参阅 OsgiJersey 项目。