Jersey Provider没有以编程方式获取Context

时间:2017-06-01 10:20:47

标签: java web-services jersey jax-rs

我在Jersey中以编程方式启动了REST Web服务,我需要它来为2h提供程序打包以进行AuthN / AuthZ目的。问题是2PP Provider无法解析@Context。我在下面提供了正在执行的代码的示例片段。

需要整合的提供商

package com.fctest.restful.hello.provider;

@Component (immediate = true)
@javax.ws.rs.ext.Provider
public class MyDummyProvider
{
    @Context
    protected javax.inject.Provider<ContainerRequestContext> containerRequestContextProvider;
    //The above var is NULL causing calls to fail for this class.
}

我的网络服务代码

@Activate
public void activate(BundleContext bctx)
{
    registerServlet(bctx);
    registerDummyResource();
    packages("com.fctest.restful.hello.provider");
    myContainer.reload(this);
}

private void registerServlet(BundleContext bctx)
{
    try
    {
        HttpService httpService = bctx.getService(bctx.getServiceReference(HttpService.class));
        Dictionary<String, String> jerseyServletParams = new Hashtable<String, String>();
        jerseyServletParams.put("javax.ws.rs.Application", NoAnnotateRest.class.getName());
        httpService.registerServlet("/services", myContainer, jerseyServletParams, null);
    }
    catch (Exception e)
    {
        LOG.error("Exception occurred registering servlet: ", e);
    }
}

private void registerDummyResource()
{
    final Resource.Builder resourceBuilder = Resource.builder();
    resourceBuilder.path("/hello2");

    final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET");
    methodBuilder
            .produces(MediaType.TEXT_PLAIN_TYPE)
            .handledBy(new Inflector<ContainerRequestContext, String>()
            {
                @Override
                public String apply(ContainerRequestContext containerRequestContext)
                {
                    return "Hello World!";
                }
            });

    registerResources(resourceBuilder.build());
}

我也尝试过以下错误:

@Activate
public void activate(BundleContext bctx)
{
    registerServlet(bctx);
    registerDummyResource();
    packages("com.fctest.restful.hello.provider");
    register(MyDummyProvider.class);
    myContainer.reload(this);
}
  

2017-06-01 17:03:45,025 |警告| raf-3.0.7 / deploy |供应商
  | 75 - org.glassfish.jersey.core.jersey-common - 2.17.0 |提供者   在SERVER中注册的com.fctest.restful.hello.provider.MyDummyProvider   运行时没有实现适用于的任何提供程序接口   SERVER运行时。由于提供者的约束配置问题   com.fctest.restful.hello.provider.MyDummyProvider将被忽略。

1 个答案:

答案 0 :(得分:0)

在我的Spring-Boot应用程序中,我遇到了类似的警告,解决方案是不注册object,而是注册object.getClass()

在你的情况下,我会替换

registerResources(resourceBuilder.build());

register(resourceBuilder.build().getClass());