【问题标题】:jersey declarative hyperlinking customizing the resulting url球衣声明性超链接自定义生成的 url
【发布时间】:2015-09-22 15:00:10
【问题描述】:

我已经设法让球衣声明式超链接工作,但是,我希望得到这段代码的结果:

@InjectLink(resource = UserResource.class, method="getUserApplications",  bindings ={@Binding(name = "id", value = "${instance.id}")}, style =  InjectLink.Style.ABSOLUTE)
private String applications;

是这样的:

    "account": {
    "applications":{ "href":"https://localhost:8080/IMREST/api/accounts/55fb111629d40123a811ad0e/applications"}
}

而不是这个:

    "account": {
    "applications" : "http://localhost:8080/IMREST/api/accounts/55fb111629d40123a811ad0e/applications"
}

有谁知道我可以如何做到这一点?

我在想我可以扩展@InjectLink 接口,但我不确定这是否可行。我目前在 Glassfish 中使用 JacksonFeature 来编组我的模型对象,到目前为止我看到的声明式超链接的唯一自定义是使用 Wrapper Objects。我宁愿避开那条路线。

【问题讨论】:

    标签: jersey jax-rs


    【解决方案1】:

    对于任何正在寻找的人,我在另一个问题(第二个答案)上找到了答案here from peeskillet

    TL;DR 我需要实现一个 LinkSerializer,因此我将其作为一个模块:

    public class LinkSerializerModule extends SimpleModule {
    
        public LinkSerializerModule() {
            super("LinkSerializerModule", new Version(0, 1, 0, "alpha", "net.galtech.id", "IMREST"));
            this.addSerializer(Link.class, new LinkSerializer());
        }
    
        public class LinkSerializer extends JsonSerializer<Link> {
    
            @Override
            public void serialize(Link link, JsonGenerator jg, SerializerProvider sp)
                    throws IOException, JsonProcessingException {
                jg.writeStartObject();
                jg.writeStringField("href", link.getUri().toString());
                jg.writeEndObject();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 2015-06-16
      • 1970-01-01
      • 2015-07-09
      相关资源
      最近更新 更多