@生产/提供者媒体类型匹配 [英] @Produces/provider media type matching

查看:94
本文介绍了@生产/提供者媒体类型匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试api版本化,并且有一个非常特殊的要求可以进行对抗.我们将为此使用内容协商,即@Produces注释,我想使用@Produces({"th/v1-v10 + xml"})这样的格式的自定义媒体类型,其中v1-v10告诉该api

I am experimenting with api verioning and have a very peculiar requirement to work against. We are going to use content-negotiation i.e @Produces annotation for this and I want to a custom media type in a format like @Produces({"th/v1-v10+xml"}), where v1-v10 tells that this api will serve any request with Accept header of "th/v1+xml", "th/v2+xml" all the way to "th/v10+xml".

我知道这有点奇怪,但是我们的想法是,我们在生产中所做的每个滴都是客户端的一个新版本,但是并不是每个服务都会被修改.因此,我想用一个范围注释该服务,以便即使它没有更改,也不必为每个下降都重复它.

I know this is a bit strange, but the idea is that each drop we make in production will be a new version for the client, but not every service will be modified. So I want to annotate the service with a range so that I don’t have to duplicate it for every drop even if it’s not changed.

所以我想找出什么方法可以在Jersey匹配@Path和@Produces批注时拦截泽西岛的登录信息?我知道我不能使用正则表达式来匹配媒体类型.

So what i want to find out is there any way I can intercept the login in Jersey while it matched the @Path and @Produces annotations? I know I can’t use regex to match media types.

.......

更多研究表明,Jersey调用MediaType.isCompatible(MediaType other)方法来确定请求接受标头和服务提供商媒体类型之间的兼容性.

A bit more research tells me that the Jersey calls the MediaType.isCompatible(MediaType other) method to determine the compatibility between the requests accept header and the services provider media type.

如果我可以创建自定义MediaType并覆盖isCompatible方法,则可能可以利用这一点.泽西岛允许这种扩展吗?

Is may be able to leverage this a bit if I can create a custom MediaType and override the isCompatible method. Does Jersey allows such extension??

我们非常感谢您的帮助.

Any help is much appreciated.

推荐答案

您可能应该使用自定义响应映射器.

You probabily should have to use a custom response mapper.

1.-创建一个实现MessageBodyWriter的类,负责编写响应

1.- Create a class implementing MessageBodyWriter in charge of writing the response

@Provider
public class MyResponseTypeMapper 
  implements MessageBodyWriter<MyResponseObjectType> {
     @Override
     public boolean isWriteable(final Class<?> type,final Type genericType,
                final Annotation[] annotations,
                final MediaType mediaType) {
       ... use one of the arguments (either the type, an annotation or the MediaType)
           to guess if the object shoud be written with this class
     }
     @Override
     public long getSize(final MyResponseObjectType myObjectTypeInstance,
                     final Class<?> type,final Type genericType,
                         final Annotation[] annotations,
                     final MediaType mediaType) {
        // return the exact response length if you know it... -1 otherwise
        return -1;
    }
    @Override
    public void writeTo(final MyResponseObjectType myObjectTypeInstance,
                    final Class<?> type,final Type genericType,
                    final Annotation[] annotations, 
                    final MediaType mediaType,
                    final MultivaluedMap<String,Object> httpHeaders,
                    final OutputStream entityStream) throws IOException,                                                                 WebApplicationException {
        ... serialize / marshall the MyResponseObjectType instance using
            whatever you like (jaxb, etC)
        entityStream.write(serializedObj.getBytes());
    }
}

2.-在您的应用程序中注册映射器

2.- Register the Mappers in your app

public class MyRESTApp 
     extends Application  {
    @Override
public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(MyResponseTypeMapper.class);
        return s;
    }
}

Jersey将扫描所有已注册的Mappers调用其isWriteable()方法,直到返回true为止.如果是,则此MessageBodyWriter实例将用于将内容序列化到客户端

Jersey will scan all registered Mappers calling their isWriteable() method until one returns true... if so, this MessageBodyWriter instance will be used to serialize the content to the client

这篇关于@生产/提供者媒体类型匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆