1

I'm implementing a ContainerResponseFilter that would add hypermedia links to the response.

The method signature in the ContainerResponseFilter is:

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

Unfortunately ContainerResponseContext does not allow me to set a Response object, and while there are getLinks() methods, there are not addLink(Link) or setLinks(Link...) methods.

I tried

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

but that resulted in an exception that said they could find a MessageBodyWriter for ResponseImpl. Also tried

responseContext.getLinks().add(link);

which doesn not work either.

Anyone ever done this?

Mordechai
  • 15,437
  • 2
  • 41
  • 82
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

2 Answers2

0

You should inject:

@Context HttpServletResponse r;

as a local field. All changes should be done through there.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
0

So I found a way to do this, by replacing the entity:

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

Not certain this is a 100% correct, but it seems to work.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84