Java Jersey Declarative Hyperlinking @Ref Annotation Use(Java Jersey 声明式超链接 @Ref 注解使用)
问题描述
我一直在尝试扩展 Jersey 1.12 文档第 6 章(声明性超链接)中提供的示例,但在使用 @Ref 注释方面似乎遇到了困难.
I have been trying to expand upon the example provided in Chapter 6 (Declarative Hyperlinking) of the Jersey 1.12 documentation but appear to have hit a wall with regard to the use of the @Ref annotation.
我的代码如下:
@Path("/offerings/{offeringId}/widgets")
@Produces(MediaType.APPLICATION_JSON)
public class WidgetsResource {
@GET
@Path("/{widgetId}")
public Response get(@PathParam("offeringId") String offeringId, @PathParam("widgetId") String widgetId) {
Widgets widgets = new Widgets();
widgets.setOfferingId(Integer.valueOf(offeringId));
Widget widget = new Widget();
widget.setId(Integer.valueOf(widgetId));
widgets.setWidgets(Arrays.asList(widget));
return Response.status(200).entity(widgets).build();
}
}
public class Widgets {
@Ref(resource = WidgetsResource.class, style=Style.ABSOLUTE)
URI uri;
@JsonIgnore
private int offeringId;
private Collection<Widget> widgets;
public Collection<Widget> getWidgets() {
return widgets;
}
public void setWidgets(Collection<Widget> widgets) {
this.widgets = widgets;
}
public URI getUri() {
return uri;
}
public int getOfferingId() {
return offeringId;
}
public void setOfferingId(int id) {
this.offeringId = id;
}
}
public class Widget {
@Ref(resource = WidgetsResource.class, style=Style.ABSOLUTE, bindings={
@Binding(name="offeringId", value="${entity.offeringId}")}
)
URI uri;
private int id;
public URI getUri() {
return uri;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
这适用于为 Widgets 集合对象的实例生成的 URL:
This works fine for the URL generated for an instance of the Widgets collection object:
"uri": "http://localhost:65080/<app>/offerings/9999/widgets"
但是,我想知道如何将集合中的 Widget 实例的 id 附加到每个 Widget 的 URL.因此,生成的 URI 将类似于:
However, I want to know how I can append the id of the Widget instances within the Collection to the URL for each Widget. So,the URI generated would be something like:
"uri": "http://localhost:65080/<app>/offerings/9999/widgets/1234"
如果不开始硬编码 Widget 类中的整个路径值,我似乎找不到使用 Ref 注释的方法来实现这一点,如果可能的话,我想避免这种情况.
I can't seem to find a way of using the Ref annotation to achieve this without starting to hardcode the whole path value within the Widget class, which I'd like to avoid if possible.
是否有实现此目的的标准方法?
Is there a standard way of achieving this?
推荐答案
我对该文档的阅读表明你可以做这样的事情(未经测试!):
My reading of that documentation says you could do something like this (untested!):
@Ref(value="/offerings/{offeringId}/widgets/{widgetId}", style=ABSOLUTE)
URI uri;
这篇关于Java Jersey 声明式超链接 @Ref 注解使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Jersey 声明式超链接 @Ref 注解使用
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01