在Jersey中实现JSONP拦截器

在Jersey中实现JSONP拦截器,jersey,jsonp,jersey-2.0,Jersey,Jsonp,Jersey 2.0,我想实现一个JsonP拦截器,我正在使用Jersey。 (我正在使用带有长轮询的AsyncResponses,我的REST方法返回'void',因此我不能用@JSONP注释它) 我的问题是我不知道如何获取查询参数。我需要知道“回调”方法的名称 我还尝试了一个常规的Servlet过滤器。它工作了,但奇怪的是我得到了methodname(){myjson}而不是 medhodname({myjson}) 所以我试着用新泽西的方式。似乎我需要一个WriterInterceptor,但如何获取查询参数

我想实现一个JsonP拦截器,我正在使用Jersey。 (我正在使用带有长轮询的AsyncResponses,我的REST方法返回'void',因此我不能用@JSONP注释它)

我的问题是我不知道如何获取查询参数。我需要知道“回调”方法的名称

我还尝试了一个常规的Servlet过滤器。它工作了,但奇怪的是我得到了
methodname(){myjson}
而不是
medhodname({myjson})

所以我试着用新泽西的方式。似乎我需要一个WriterInterceptor,但如何获取查询参数

这是我的密码:

@Provider
public class JsonpResponseFilter implements WriterInterceptor {

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
        final String callback =  (String)context.getProperty("callback");
        if (null != callback) {         
            context.getOutputStream().write((callback+"(").getBytes());
        }
        context.proceed();
        if (null != callback) {         
            context.getOutputStream().write(')');
        }
    }
}
编辑:

我找到了一种获取查询参数的方法,但对我来说这似乎是一种黑客行为(见下文)。一定有更简单或更优雅的东西。有什么想法吗

@Provider
public class JsonpResponseFilter implements WriterInterceptor {

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

        final ServiceLocator locator = ServiceLocatorClientProvider.getServiceLocator(context);
        ContainerRequestContext tmp = locator.getService(ContainerRequestContext.class);
        List<String> callbacks = tmp.getUriInfo().getQueryParameters().get("callback");
        String callback = (null == callbacks)? null:callbacks.get(0);
        ...
@Provider
公共类JsonpResponseFilter实现WriterInterceptor{
@凌驾
WriteTo(WriterInterceptorContext上下文)周围的public void引发IOException、WebApplicationException{
最终ServiceLocator定位器=ServiceLocatorClientProvider.getServiceLocator(上下文);
ContainerRequestContext tmp=locator.getService(ContainerRequestContext.class);
List callbacks=tmp.getUriInfo().getQueryParameters().get(“回调”);
字符串回调=(null==回调)?null:callbacks.get(0);
...

一个不太老套的解决方案是将提供者作为字段注入类中:

@Inject
private Provider<ContainerRequest> containerRequestProvider;
@Inject
私有提供者containerRequestProvider;
从这里,您可以访问如下查询参数:

final ContainerRequest containerRequest = containerRequestProvider.get();
final UriInfo uriInfo = containerRequest.getUriInfo();
final MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
final List<String> queryParameter = queryParameters.get("q");
...
final ContainerRequest ContainerRequest=containerRequestProvider.get();
final UriInfo UriInfo=containerRequest.getUriInfo();
最终多值Map queryParameters=uriInfo.getQueryParameters();
最终列表queryParameter=queryParameters.get(“q”);
...

谢谢。你能给我指出一些文档/最好是教程,教我@Inject的原理和用法吗?