Han*_*ank 7 java dependency-injection jersey
我按照这篇文章,由第一个自定义注入提供程序创建,LocaleProvider
它扫描HTTP请求以获取Accept-Language标头.如果我在Jersey资源中使用它,它可以正常工作:
@Path("test")
@GET
public Response getStatus(@Context Locale locale) {
log.finer("Locale: "+ locale);
Response response = Response
.noContent()
.build();
return response;
}
Run Code Online (Sandbox Code Playgroud)
但是当我想在它中使用它时ExceptionMapper
我不知道如何注入语言环境.我试图将它注释为成员变量:
public class MyExceptionMapper implements ExceptionMapper<MyException> {
@Context
private Locale locale;
@Override
public Response toResponse(MyException ex) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
但这在部署时失败:
The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for field: private java.util.Locale com.package.MyExceptionMapper.locale
Run Code Online (Sandbox Code Playgroud)
其他类似UriInfo
或HttpServletRequest
可以注入的东西.如何访问我自己的LocaleProvider?
仅供参考,使用Jersey 1.8进行Glassfish 3.1.1.
存在范围不匹配。ExceptionMapper 是一个单例,而您的可注入是每个请求范围的。UriInfo 使用 ThreadLocals 解决这个问题。
归档时间: |
|
查看次数: |
2868 次 |
最近记录: |