Browse Source

新增版本支持

chenweida 7 years ago
parent
commit
6ef0a11452

+ 23 - 0
web-gateway/src/main/java/com/yihu/jw/config/MvcConfig.java

@ -0,0 +1,23 @@
package com.yihu.jw.config;
import com.yihu.jw.version.JWRequestMappingHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
/**
 * Created by chenweida on 2017/6/15.
 */
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = new JWRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);//顺序是第一
        handlerMapping.setInterceptors(getInterceptors());
        return handlerMapping;
    }
}

+ 21 - 1
web-gateway/src/main/java/com/yihu/jw/controller/PatientController.java

@ -3,6 +3,7 @@ package com.yihu.jw.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yihu.jw.fegin.PatientFegin;
import com.yihu.jw.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@ -17,11 +18,13 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.SessionScope;
import javax.servlet.http.HttpServletRequest;
/**
 * Created by chenweida on 2017/5/10.
 */
@RestController
@RequestMapping("/rest/patient")
@RequestMapping("/{version}/patient")
@Api(description = "患者")
public class PatientController {
    private Logger logger= LoggerFactory.getLogger(PatientController.class);
@ -56,4 +59,21 @@ public class PatientController {
//    public String findByCodeFallback(String code) {
//        return "启动断路器";
//    }
    @GetMapping("/hello")
    @ApiVersion(1)
    @ResponseBody
    public String hello1(HttpServletRequest request){
        System.out.println("haha1..........");
        return "hello";
    }
    @GetMapping("/hello")
    @ApiVersion(2)
    @ResponseBody
    public String hello2(HttpServletRequest request){
        System.out.println("haha2.........");
        return "hello";
    }
}

+ 21 - 0
web-gateway/src/main/java/com/yihu/jw/version/ApiVersion.java

@ -0,0 +1,21 @@
package com.yihu.jw.version;
import org.springframework.web.bind.annotation.Mapping;
import java.lang.annotation.*;
/**
 * 版本控制的注解
 * Created by chenweida on 2017/6/15.
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface ApiVersion {
    /**
     * 版本号
     * @return
     */
    int value();
}

+ 47 - 0
web-gateway/src/main/java/com/yihu/jw/version/ApiVesrsionCondition.java

@ -0,0 +1,47 @@
package com.yihu.jw.version;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 版本控制的规则类
 * Created by chenweida on 2017/6/15.
 */
public class ApiVesrsionCondition implements RequestCondition<ApiVesrsionCondition> {
    // 路径中版本的前缀, 这里用 /v[1-9]/的形式
    private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/");
    private int apiVersion;
    public ApiVesrsionCondition(int apiVersion){
        this.apiVersion = apiVersion;
    }
    public ApiVesrsionCondition combine(ApiVesrsionCondition other) {
        // 采用最后定义优先原则,则方法上的定义覆盖类上面的定义
        return new ApiVesrsionCondition(other.getApiVersion());
    }
    public ApiVesrsionCondition getMatchingCondition(HttpServletRequest request) {
        String path=request.getRequestURI();
        Matcher m = VERSION_PREFIX_PATTERN.matcher(path);
        if(m.find()){
            Integer version = Integer.valueOf(m.group(1));
            if(version >= this.apiVersion) // 如果请求的版本号大于配置版本号, 则满足
                return this;
        }
        return null;
    }
    public int compareTo(ApiVesrsionCondition other, HttpServletRequest request) {
        // 优先匹配最新的版本号
        return other.getApiVersion() - this.apiVersion;
    }
    public int getApiVersion() {
        return apiVersion;
    }
}

+ 29 - 0
web-gateway/src/main/java/com/yihu/jw/version/JWRequestMappingHandlerMapping.java

@ -0,0 +1,29 @@
package com.yihu.jw.version;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
/**
 * Created by chenweida on 2017/6/15.
 * 扩展spring的RequestMappingHandlerMapping
 */
public class JWRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected RequestCondition<ApiVesrsionCondition> getCustomTypeCondition(Class<?> handlerType) {
        ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
        return createCondition(apiVersion);
    }
    @Override
    protected RequestCondition<ApiVesrsionCondition> getCustomMethodCondition(Method method) {
        ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
        return createCondition(apiVersion);
    }
    private RequestCondition<ApiVesrsionCondition> createCondition(ApiVersion apiVersion) {
        return apiVersion == null ? null : new ApiVesrsionCondition(apiVersion.value());
    }
}