首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jersey 2:如何创建自定义HTTP参数绑定

jersey 2:如何创建自定义HTTP参数绑定
EN

Stack Overflow用户
提问于 2014-04-23 06:35:16
回答 4查看 6.3K关注 0票数 17

我正在尝试为我的restful服务创建一个自定义的http参数绑定。请参考下面的示例。

@POST
@Path("/user/{userId}/orders")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public MyResult foo(@PathParam("userId") String someString, @UserAuthHeaderParam String authString){

}

您可以看到,在函数签名中有一个UserAuthHeaderParam注释。我想做的是有一个自定义的http参数绑定,而不是标准的javax.ws.rs.*Param。

我已经尝试实现了org.glassfish.hk2.api.InjectionResolver,它基本上是从http头中提取值:

public class ProtoInjectionResolver implements InjectionResolver<UserAuthHeaderParam>{
...
@Override
public Object resolve(Injectee injectee, ServiceHandle< ? > root)
{

    return "Hello World";
}
...

}

当我调用restful服务时,服务器出现以下异常。表示框架无法解析函数签名中的param:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=String,parent=MyResource,qualifiers={}),position=0,optional=false,self=false,unqualified=null,2136594195), 

java.lang.IllegalArgumentException: While attempting to resolve the dependencies of rs.server.MyResource errors were found

请帮帮忙。任何建议都是非常感谢的。我确实在谷歌上做了很多搜索,但都不能让它工作。Jersey 2.*. How to replace InjectableProvider and AbstractHttpContextInjectable of Jersey 1.*可能是类似的问题。

--更新:我使用AbstractBinder将我的解析器绑定到UserAuthHeaderParam:

public class MyApplication extends ResourceConfig
{

public MyApplication()
{
    register(new AbstractBinder()
    {
        @Override
        protected void configure()
        {
            // bindFactory(UrlStringFactory.class).to(String.class);
            bind(UrlStringInjectResolver.class).to(new TypeLiteral<InjectionResolver<UrlInject>>()
            {
            }).in(Singleton.class);
        }
    });
    packages("rs");

}

}

谢谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23231511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档