从Jersey WriterInterceptor获取URL

时间:2018-11-28 09:37:46

标签: java rest jersey interceptor

我知道有一种方法可以在Jersey的ContainerRequestFilter类中获取URL。

是否有一种方法可以从aroundWriteTo方法中的writerInterceptor获取URL?

@Provider
public class RequestClientWriterInterceptor implements WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) 
  throws IOException, WebApplicationException {
    context.getOutputStream()
      .write(("Message added in the writer interceptor in the client side").getBytes());

    context.proceed();
}
}

3 个答案:

答案 0 :(得分:0)

您还可以使用新的标题名称发送URL,然后使用getHeaders获取特定的标题:

 MultivaluedMap<String,Object> headers = context.getHeaders();

答案 1 :(得分:0)

您只需将UriInfo注入拦截器即可。您可以从该对象获取所需的所有与URI相关的东西。

@Provider
public class RequestClientWriterInterceptor implements WriterInterceptor {

    @Context
    private UriInfo uriInfo;
}

您不必担心任何竞争条件或任何此类情况,因为将注入线程本地代理,而不是实际的对象。

答案 2 :(得分:0)

我通过从上下文属性中检索HttpChannel,然后从HttpChannel获取请求来获得URL。请参阅以下...。

@Override
public void aroundWriteTo(WriterInterceptorContext context) 
  throws IOException, WebApplicationException {

    HttpChannel httpChannel = (HttpChannel) context.getProperty(HttpChannel.class.getName());
    String url = httpChannel.getRequest().getOriginalURI());

    context.getOutputStream()
      .write(("Message added in the writer interceptor in the client side").getBytes());

    context.proceed();
}