【发布时间】:2025-11-28 06:20:03
【问题描述】:
我正在尝试为我的 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 服务时,服务器出现以下异常。表示框架解析函数签名中的参数失败:
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");
}
}
谢谢!
【问题讨论】:
-
@Service 在哪里?
-
@MingtaoZhang 我注册了一个 AbstractBinder。编辑我的问题以添加此详细信息
-
您使用的是哪个版本的球衣罐?你是在任何应用服务器或 grizzly 上运行它吗?
-
@DhanaKrishnasamy 我正在使用 jersey 2.0 和 grizzly。
-
@yzandrew 为什么要创建自定义 Http 参数?也许您可以使用
@HeaderParam并通过 HTTP 标头将 UserAuth 值传递给您的 REST API?
标签: java jersey-2.0 grizzly