2021-11-25 接口设计

 

接口设计

@Data

public class APIResponse<T> {

    private boolean success;

    private T data;

    private int code;

    private String message;

}

版本控制,定义接口版本

//通过URL Path实现版本控制

@GetMapping("/v1/api/user")

public int right1() {   

return 1;

}

首先,创建一个注解来定义接口的版本。@APIVersion 自定义注解可以应用于方法或 Controller 上:

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface APIVersion {

    String[] value();

}

然后,定义一个 APIVersionHandlerMapping 类继承 RequestMappingHandlerMapping。

public class APIVersionHandlerMapping extends RequestMappingHandlerMapping {

    @Override

    protected boolean isHandler(Class<?> beanType) {

        return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);

    }

    @Override

    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {

        Class<?> controllerClass = method.getDeclaringClass();

        //类上的APIVersion注解

        APIVersion apiVersion = AnnotationUtils.findAnnotation(controllerClass, APIVersion.class);

        //方法上的APIVersion注解

        APIVersion methodAnnotation = AnnotationUtils.findAnnotation(method, APIVersion.class);

        //以方法上的注解优先

        if (methodAnnotation != null) {

            apiVersion = methodAnnotation;

        }

        String[] urlPatterns = apiVersion == null ? new String[0] : apiVersion.value();

      

        PatternsRequestCondition apiPattern = new PatternsRequestCondition(urlPatterns);

        PatternsRequestCondition oldPattern = mapping.getPatternsCondition();

        PatternsRequestCondition updatedFinalPattern = apiPattern.combine(oldPattern);

        //重新构建RequestMappingInfo

        mapping = new RequestMappingInfo(mapping.getName(), updatedFinalPattern, mapping.getMethodsCondition(),

                mapping.getParamsCondition(), mapping.getHeadersCondition(), mapping.getConsumesCondition(),

                mapping.getProducesCondition(), mapping.getCustomCondition());

        super.registerHandlerMethod(handler, method, mapping);

    }

}

最后,也是特别容易忽略的一点,要通过实现 WebMvcRegistrations 接口,来生效自定义的 APIVersionHandlerMapping。

@SpringBootApplication

public class CommonMistakesApplication implements WebMvcRegistrations {

...

    @Override

    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {

        return new APIVersionHandlerMapping();

    }

}

这样,就实现了在 Controller 上或接口方法上通过注解,来实现以统一的 Pattern 进行版本号控制。

@GetMapping(value = "/api/user")

@APIVersion("v4")

public int right4() {

    return 4;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值