jersey 2: How to create custom HTTP param binding

后端 未结 4 2130
醉梦人生
醉梦人生 2020-12-05 15:08

I am trying to create a custom http param binding for my restful service. Please see the example below.

@POST
@Path(\"/user/{userId}/orders\")
@Produces(Medi         


        
4条回答
  •  半阙折子戏
    2020-12-05 15:22

    If all you want is to pass value directly from the header to the method you don't need to create custom annotations. Let's say you have a header Authorization, then you can easily access it by declaring your method like this:

    @GET
    public String authFromHeader(@HeaderParam("Authorization") String authorization) {
        return "Header Value: " + authorization + "\n";
    }
    

    You can test it by calling curl, e.g.

    $ curl --header "Authorization: 1234" http://localhost:8080/rest/resource
    Header Value: 1234
    

    Given that the answer to your question, how to create custom binding is as follows.

    First you have to declare your annotation like this:

    @java.lang.annotation.Target(PARAMETER)
    @java.lang.annotation.Retention(RUNTIME)
    @java.lang.annotation.Documented
    public @interface UserAuthHeaderParam {
    }
    

    Having your annotation declared you have to define how it will be resolved. Declare the Value Factory Provider (this is where you'll have access to the header parameters - see my comment):

    @Singleton
    public class UserAuthHeaderParamValueFactoryProvider extends AbstractValueFactoryProvider {
    
        @Inject
        protected UserAuthHeaderParamValueFactoryProvider(MultivaluedParameterExtractorProvider mpep, ServiceLocator locator) {
            super(mpep, locator, Parameter.Source.UNKNOWN);
        }
    
        @Override
        protected Factory createValueFactory(Parameter parameter) {
            Class classType = parameter.getRawType();
    
            if (classType == null || (!classType.equals(String.class))) {
                return null;
            }
    
            return new AbstractHttpContextValueFactory() {
                @Override
                protected String get(HttpContext httpContext) {
                    // you can get the header value here
                    return "testString";
                }
            };
        }
    }
    

    Now declare an injection resolver

    public class UserAuthHeaderParamResolver extends ParamInjectionResolver {
        public UserAuthHeaderParamResolver() {
            super(UserAuthHeaderParamValueFactoryProvider.class);
        }
    }
    

    and a Binder for your configuration

    public class HeaderParamResolverBinder extends AbstractBinder {
    
        @Override
        protected void configure() {
            bind(UserAuthHeaderParamValueFactoryProvider.class)
                    .to(ValueFactoryProvider.class)
                    .in(Singleton.class);
    
            bind(UserAuthHeaderParamResolver.class)
                    .to(new TypeLiteral>() {})
                    .in(Singleton.class);
        }
    }
    

    now the last thing, in your ResourceConfig add register(new HeaderParamResolverBinder()), like this

    @ApplicationPath("rest")
    public class MyApplication extends ResourceConfig {
        public MyApplication() {
            register(new HeaderParamResolverBinder());
            packages("your.packages");
        }
    }
    

    Given that, you should be now able to use the value as you wanted:

    @GET
    public String getResult(@UserAuthHeaderParam String param) {
        return "RESULT: " + param;
    }
    

    I hope this helps.

提交回复
热议问题