开发者

Jersey: how to use InjectableProvider with @Context annotation?

开发者 https://www.devze.com 2023-04-10 20:14 出处:网络
I followed this post and created by first custom injectable provider, a LocaleProvider that scans the HTTP Request for the Accept-Language header. It works fine if I use it within a Jersey resource:

I followed this post and created by first custom injectable provider, a LocaleProvider that scans the HTTP Request for the Accept-Language header. It works fine if I use it within a Jersey resource:

@Path("test")
@GET
public Response getStatus(@Context Locale locale) {
    log.finer("Locale: "+ locale);

    Response response = Response
            .noContent()
            .build();
    return response;
}

But when I want to use it in an ExceptionMapper I don't know how to inject the locale. I've tried to annotate it as a member variable:

public class MyExceptionMapper implements ExceptionMapper<MyException> {
  @Context
  private Locale 开发者_StackOverflow社区locale;

  @Override
  public Response toResponse(MyException ex) {
    ...
  }
}

but this fails at deployment:

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

Other things like UriInfo or HttpServletRequest can be injected like that. How do I get access to my own LocaleProvider?

FYI this is on Glassfish 3.1.1 using Jersey 1.8.


There is a scope mismatch. ExceptionMapper is a singleton, while your injectable is per-request-scope. UriInfo works around it using ThreadLocals.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号