users@jersey.java.net

Re: [Jersey] Re: Adding _at_Provider via META-INF/services, injecting @Context Providers?

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Mon, 27 Apr 2009 09:51:44 -0700

On Mon, Apr 27, 2009 at 4:12 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
...
>> My problem seems to be the next step; ensuring that my
>> ContextProvider<T> matches with call. Looks like MediaType doesn't
>> match by default for some reason (if I pass type I get in).
>
> Can you show me the implementation code?

Sure. I'll attach the full file, but here's the snippet I think is relevant:

---
    protected ObjectMapper _getMapper(Class<?> type, MediaType mediaType)
    {
        // First: were we configured with an instance?
        if (_configuredMapper != null) {
            return _configuredMapper;
        }
        // If not, maybe we can get one configured via context?
        ContextResolver<ObjectMapper> resolver =
_providers.getContextResolver(ObjectMapper.class, mediaType);
        // Not registered with media type, how about without?
        if (resolver == null) {
            resolver = _providers.getContextResolver(ObjectMapper.class, null);
        }
---
Oh, and the other part is the provider for ObjectMapper:
---
@Provider
/* Produces ObjectMappers for all types...
 * not sure what's the magic incantation; default should be "any types"
 */
@Produces
public final class ObjectMapperHolder
    implements ContextResolver<ObjectMapper>
{
   //....
    public ObjectMapper getContext(Class<?> type)
    {
        if (type == ObjectMapper.class) { // should always be true
            return _mapper;
        }
        return null;
    }
}
---
So it should be some mismatch between media types... but I was unable
to declare proper type in @Produces of provider.
-+ Tatu +