Java Jersey 声明式超链接 @Ref 注解使用

Java Jersey Declarative Hyperlinking @Ref Annotation Use(Java Jersey 声明式超链接 @Ref 注解使用)

本文介绍了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 注解使用

基础教程推荐