Java的球衣声明超链接@Ref使用注释 [英] Java Jersey Declarative Hyperlinking @Ref Annotation Use

查看:426
本文介绍了Java的球衣声明超链接@Ref使用注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在泽西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.

我的code是如下:

@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集合对象的实例生成的网址:

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集合内的网址为每个插件。因此,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"

我似乎无法找到使用参考注解来实现这一目标的一种方式,而无需启动硬code Widget类,我想尽量避免其内的整个路径值。

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的球衣声明超链接@Ref使用注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆