将@Context参数传递给类中的方法

时间:2016-07-26 19:20:37

标签: java spring javax.ws.rs

我有一个现有的类,我正试图将一些头参数提供给SSO用户进入我们的系统。课程如下。

import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

@Component
@Path("/http")
public class HttpResource {
    @GET
    @Path("/getHeaders")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> getAllHeaders(@Context HttpHeaders headers) {
        Map<String, String> headerList = new HashMap<String, String>();
        for (String key : headers.getRequestHeaders().keySet()) {
            String value = headers.getRequestHeader(key).get(0);
            System.out.println(key + " : " + value);
            headerList.put(key, value);
        }
        return headerList;
    }
}

我想弄清楚的是如何使用getAllHeaders()参数调用@Context?我已经找到了很多关于我所拥有的课程的例子,但是没有任何关于如何称呼它的例子。

我也尝试将注释放在类中而不是作为参数。

@Context
HttpHeaders httpHeaders;

但是当我尝试访问httpHeaders.getAllHeaders()时,它返回null。我假设因为它实际上并没有创建,因为javax文档说它永远不会返回null。

我试图在我的SSOAuthorizationFilter.java中调用它,但也尝试通过控制器访问它。

1 个答案:

答案 0 :(得分:5)

首先编写注释。

@Retention(RUNTIME)
@Target({ PARAMETER })
@Documented
public @interface SSOAuthorization {}

然后是解决方案。

public class SSOAuthorizationResolver {

    public static class SSOAuthorizationInjectionResolver extends
            ParamInjectionResolver<SSOAuthorization> {
        public SSOAuthorizationInjectionResolver() {
            super(SSOAuthorizationValueFactoryProvider.class);
        }
    }


    @Singleton
    public static class SSOAuthorizationValueFactoryProvider extends
            AbstractValueFactoryProvider {

        @Context
        private HttpHeaders httpHeaders;

        @Inject
        public SSOAuthorizationValueFactoryProvider(
                final MultivaluedParameterExtractorProvider mpep,
                final ServiceLocator injector) {
            super(mpep, injector, Parameter.Source.UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (!Language.class.equals(classType)
                    || parameter.getAnnotation(SSOAuthorization.class) == null) {
                return null;
            }

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    // Can use httpHeader to get your header here.
                    return httpHeader.getHeaderString("SSOAuthorization");
                }
            };
        }

    }

    // Binder implementation
    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {

            bind(SSOAuthorizationValueFactoryProvider.class).to(
                    ValueFactoryProvider.class).in(Singleton.class);

            bind(SSOAuthorizationInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<SSOAuthorization>>() {
                    }).in(Singleton.class);

        }

    }
}

在ResourceConfig中只需注册Resolver

public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig(Class... classes) {
        super(classes);
        register(new SSOAuthorizationResolver.Binder());
    }
}

最后使用@SSOAuthorization注释在控制器中使用它。

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public String someMethod(@SSOAuthorization String auth) {
    return auth;
}