JWRequestMappingHandlerMapping.java 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. package com.yihu.jw.version;
  2. import org.springframework.core.annotation.AnnotationUtils;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.web.servlet.mvc.condition.RequestCondition;
  5. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
  6. import java.lang.reflect.Method;
  7. /**
  8. * Created by chenweida on 2017/6/15.
  9. * 扩展spring的RequestMappingHandlerMapping
  10. */
  11. public class JWRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
  12. @Override
  13. protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {
  14. ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
  15. return createCondition(apiVersion);
  16. }
  17. @Override
  18. protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) {
  19. ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
  20. return createCondition(apiVersion);
  21. }
  22. private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) {
  23. return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value());
  24. }
  25. }