【问题标题】:JAXRS-2.0 Filter: How does one add links to ContainerResponseJAXRS-2.0 过滤器:如何将链接添加到 ContainerResponse
【发布时间】:2017-05-27 00:02:09
【问题描述】:

我正在实现一个ContainerResponseFilter,它将向响应添加超媒体链接。

ContainerResponseFilter中的方法签名是:

public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException

不幸的是ContainerResponseContext 不允许我设置Response 对象,虽然有getLinks() 方法,但没有addLink(Link)setLinks(Link...) 方法。

我试过了

responseContext.setEntity(Response.ok().links(link).build());

但这导致了一个异常,表示他们可以为ResponseImpl 找到MessageBodyWriter。也试过了

responseContext.getLinks().add(link);

这也不起作用。

有人做过吗?

【问题讨论】:

    标签: java jakarta-ee jax-rs apache-tomee hypermedia


    【解决方案1】:

    所以我找到了一种方法,通过替换实体:

    URI uri = uriInfo.getBaseUriBuilder().path(RESOURCE_CLASS).path(RESOURCE_METHOD).build(domain_object.getId());
    JaxbLink jaxbLink = new JaxbLink(uri);
    responseContext.setEntity(jaxbLink);
    

    不确定这是否 100% 正确,但它似乎有效。

    【讨论】:

      【解决方案2】:

      你应该注入:

      @Context HttpServletResponse r;
      

      作为本地字段。所有更改都应通过那里完成。

      【讨论】:

      • :/我不想直接操纵响应...我将链接编组/编码和发生的事情
      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多