问题标签 [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.

0 投票
3 回答
4038 浏览

java - JAX-RS 应用程序子类注入

我正在编写自定义 JAX-RS 2.0 应用程序(在 Jersey 2.3.1 下),它包含一些供所有资源使用的数据。

(我也可以使用 API javax.ws.rs.core.Application,描述的结果是一样的)

然后我将对象注入资源

但是,调用的结果是

这让我使用了一些丑陋的技巧,比如

我对 JAX-RS 2.0 规范第 9.2.1 节的理解:

应用程序提供的子类的实例可以使用注解Application注入到类字段或方法参数中。@Context对子类实例的访问Application允许将配置信息集中在该类中。请注意,这不能注入Application子类本身,因为这会创建循环依赖。

应用程序提供的Application子类是我WebApp的,而不是特定于 JAX-RS 实现的包装器。

另外,改变这个片段

对此

原因appnull,由于ClassCastException在上下文注入期间,所以声明的类型无关紧要。

这是泽西岛的错误还是我的误解?

更新:我检查了 RESTEasy 3.0 下的行为。注入的对象是 my WebApp,没有任何包装。我会称之为泽西岛的一个错误。

0 投票
1 回答
2401 浏览

java - Jersey 2:如何绑定注入类而不进行包扫描?

最初我确实通过这样做手动包含了我的课程

然后我发现为了能够在 RestServlet 中注入 RestService,我需要使用 ResourceConfig binder。

但是我不知道如何在不定义包的情况下使用 ResourceConfig 并为每个注入类手动绑定它?

PS我也不明白如何确保所有包裹扫描都被禁用?

0 投票
1 回答
3570 浏览

java - Jersey 2.4 使用 Spring Context Profile 进行测试 - DI 不起作用

我正在尝试使用 Spring 应用程序上下文配置 Jersey (2.4) 测试,使用 Spring 配置文件。

我的测试课是这样的:

这是哪里ApplicationResourceConfig的课:

OrderController类是:

问题是 DI 不工作(orderServicenull)。DI 在其他测试中运行良好(不是基于 Jersey 的)。我想原因可能是没有正确加载 Spring 配置文件,因为 Spring 日志说:

请问有没有人可以就这个问题提供建议?

0 投票
2 回答
16274 浏览

java - 将 Jersey 客户端 1.x 升级到 2.x 时 UriBuilder 上的 AbstractMethodError

我们正在将我们的Web 应用程序(在Tomcat7 上运行)从Jersey 1.7 升级到Jersey 2.4.1。我现在设法让服务器端正常工作,但是使用 jersey-client 的 JUnit 测试抛出 AbstractMethodError: java.lang.AbstractMethodError:

该项目由 maven 维护,以下是 pom 的依赖项:

显然我错过了 UriBuilder 类的具体实现,但我不知道我需要为此添加什么依赖项。另外——它适用于服务器端,wut!?

0 投票
0 回答
226 浏览

java - Serializing collections / aggregates and conflating models and resources in REST

I'm developing a REST API in order to transfer data from a client application to a web application.

So far it all worked well: I created my entity objects in the client application and then serialized them with Jersey into XML/JSON and called PUT, on the other end a Resource listened for PUT and magically serialized them into Objects ready to be persisted with JPA.

Below is a sample of an entity Album with annotations and the associated Resource.

The above worked well even though according to some sites this is a classic anti-pattern of "Conflating models and resources" (see for instance http://jacobian.org/writing/rest-worst-practices/). Their reasoning is that the above example would collapse, indeed to update an Album name I would need to re-send a PUT with a whole photos collection, in a more articulated example I would end sending tons of useless data just to honour relations between objects.

They however don't point out a regular solution so I came up with this rather convoluted solution to keep mapping models and resources together and thus exploit automatic serialization:

I would basically send from the client a new entity "RESTAlbum" where instead of a collection of Photos as the "Album" entity it would contain a collection of links to photos:

on the server side end I would serialize RESTAlbum and would fetch each photo entity based on its URL and construct manually an Album entity which I would be able to serialize with JPA.

Is the above the only way to go?

It seems like quite a lot of work to create all this custom classes just to serialize relations and strangely all the web is full of REST purists but noone talking about issues like this.

0 投票
1 回答
3338 浏览

jersey-2.0 - Jersey 2.0 将 HttpServletRequest 注入 ExceptionMapper

我使用 Jersey 2.3.1,并且我有以下异常映射器。

当应用程序启动时,它会抛出以下异常,说明 CustomExceptionMapper 不在请求范围内。

同样的代码在 Jersey 1.8 上运行良好,但是当我迁移到 2.3.1 时它无法正常工作。

我对此有点挣扎,任何帮助将不胜感激。

0 投票
1 回答
1018 浏览

web-services - 如何在 restFul Get 方法中传递 Employee 对象

我在 RestFul webservices Jaxrs2/jersy2 中传递一个员工对象表单客户端

如何使用 GET 方法获得这个对象?提前谢谢

0 投票
2 回答
1741 浏览

rest - 使用 Jersey 和 weblogic 的服务器发送事件

我正在试验服务器发送的事件。我正在关注此链接https://jersey.java.net/documentation/latest/user-guide.html#d0e8376。当我向代表服务器发送事件的资源发出请求时,我收到 500 内部服务器错误。

根据以下错误,我必须为 org.glassfish.jersey.media.sse.EventOutput 注册正文作者。但是,怎么做呢?

0 投票
5 回答
10307 浏览

web-services - 带有球衣的宁静服务接口

我可以创建一个带有接口和实现类的宁静服务吗?

如果是这样,所有与 JAX-RS 相关的导入都会进入接口吗?

我正在使用 jersey2.4 和 jetty8.1。

这是我的MyService界面:

以及MyServiceImpl该接口的实现

这是 web.xml 文件:

我注册了这个服务提供者包 ( foo.bar) 但它抱怨说这个

当我尝试使用实现类包(foo.bar.impl)时,它抱怨说这个

当我尝试两者时 - 它的抱怨与上述相同:

我做错了什么?

0 投票
1 回答
2052 浏览

java - Jersey、Jackson 和 JAX-RS POST 多种 JSON 格式

我试图能够定义以下代码:

我知道我需要使用:

正确设置我的对象映射器,以便能够在我的休息资源上接受单个值作为数组。我成功地设置了那部分。

我对这种方法的问题是以下内容不可区分:

将产生一个大小为 1 的列表 (List)。然后,在方法 create(List myObjects) 中,我将无法区分发送到 Rest Resource 的 List 和 Single Object。

然后,我的问题是如何做这样的事情。这个想法是只有一个@POST 可以同时接受数组和单个值?

理想情况下,我将摆脱 ObjectMapper 的配置,以避免将 Single Object 设置到 JSON 文档的其他级别。例如,我不想允许这样:

通常这种格式应该是强制性的:

基于此,如果我能够区分列表和对象之间的差异,我尝试放置我的列表的对象包装器。有这样的东西:

资源变为:

并尝试通过 JAX-RS/Jersey/Jackson 机制对我的内容进行反序列化。如果我让解决方案保持现在的样子,反序列化会失败,因为预期的 JSON 格式如下:

然后我尝试编写一个自定义反序列化器,但我在这个任务中有点迷失了。我有类似的东西:

我只想反序列化根级别以将反序列化的内容设置为对象包装器。当不同的@Provider 的配置完成时,我还想保持在一个用@ApplicationPath 注释的类中配置的特性。

我希望所有信息都能充分说明我想要做什么以及我已经测试过的内容。

等待有关如何执行在同一路径上接受数组或对象的资源的建议。

提前非常感谢。