Java 适用于不同api版本的containerRequestFilter

Java 适用于不同api版本的containerRequestFilter,java,rest,jersey,Java,Rest,Jersey,我正在创建一个API,为此我正在使用Jersey。我有一个在containerRequestFilter类中调用的身份验证机制。在那之前一切都很顺利。。 现在我正在对API进行版本控制,并成功地对所有资源进行了版本控制,但对于requestfilter,我不确定它是如何工作的。我正在使用注释进行版本控制 例如,对于登录资源,url将是/v1/signin和/v2/signin 这是使用java注释提到的,比如资源类名上方的@Path(“v1/login”) 如何以这种样式设置请求筛选器的版本 请

我正在创建一个API,为此我正在使用Jersey。我有一个在containerRequestFilter类中调用的身份验证机制。在那之前一切都很顺利。。 现在我正在对API进行版本控制,并成功地对所有资源进行了版本控制,但对于requestfilter,我不确定它是如何工作的。我正在使用注释进行版本控制

例如,对于登录资源,url将是
/v1/signin
/v2/signin
这是使用java注释提到的,比如资源类名上方的
@Path(“v1/login”)

如何以这种样式设置请求筛选器的版本

请帮帮我,我对这真的很陌生


谢谢

假设您使用的是Jersey 2.x,您可以使用,并稍微反射一下来检查
@Path
注释中的值。差不多

import javax.ws.rs.Path;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

@Provider
public class Version1Feature implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        Path classAnnotation = resourceInfo.getResourceClass().getAnnotation(Path.class);
        if (classAnnotation != null) {
            String pathValue = classAnnotation.value();
            if (pathValue != null) {
                if (pathValue.contains("v1")) {
                    context.register(Version1Filter.class);
                }
            }
        }
    }  
}
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class Version1Filter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest cr, ContainerResponse cr1) {
        String pathValue = cr.getPath();
        if (pathValue.contains("v1")) {
            cr1.getHttpHeaders().putSingle("X-Header", "Version 1.0");
        }
        return cr1;
    }
}
这将检查每个资源类的
@路径(“v1/login”)
。每个包含
“v1”
的类注释都将动态注册该类的
版本1过滤器。您还可以使用
resourceInfo.getResourceMethod().getAnnotation(Path.class)
在方法级别进行检查。为您拥有的每个资源方法调用
configure
方法

应为每个版本创建不同的功能。您也可以对所有版本使用相同的功能,如

@Provider
public class VersioningFeature implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        Path classAnnotation = resourceInfo.getResourceClass().getAnnotation(Path.class);
        if (classAnnotation != null) {
            String pathValue = classAnnotation.value();
            if (pathValue != null) {
                if (pathValue.contains("v1")) {
                    context.register(Version1Filter.class);
                } else if (pathValue.contains("v2")) {
                    context.register(Version2Filter.class);
                }
            }
        }
    }  
}
但对我来说,为每个版本实现不同的功能更有意义,因为在创建新版本时,您不需要触摸功能实现

对于Jersey 1.x,它没有那么优雅。您需要在过滤器中显式地执行检查。差不多

import javax.ws.rs.Path;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

@Provider
public class Version1Feature implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        Path classAnnotation = resourceInfo.getResourceClass().getAnnotation(Path.class);
        if (classAnnotation != null) {
            String pathValue = classAnnotation.value();
            if (pathValue != null) {
                if (pathValue.contains("v1")) {
                    context.register(Version1Filter.class);
                }
            }
        }
    }  
}
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class Version1Filter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest cr, ContainerResponse cr1) {
        String pathValue = cr.getPath();
        if (pathValue.contains("v1")) {
            cr1.getHttpHeaders().putSingle("X-Header", "Version 1.0");
        }
        return cr1;
    }
}
你也可以用Jersey 2做同样的事情,但就个人而言,我认为这个功能更优雅