36 changed files with 325 additions and 28 deletions
@ -0,0 +1,51 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<parent> |
||||
|
<artifactId>epdc-commons</artifactId> |
||||
|
<groupId>com.esua.epdc</groupId> |
||||
|
<version>1.0.0</version> |
||||
|
</parent> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<artifactId>epdc-commons-api-version-control</artifactId> |
||||
|
<packaging>jar</packaging> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-autoconfigure</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-log4j2</artifactId> |
||||
|
<scope>provided</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
<scope>provided</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-configuration-processor</artifactId> |
||||
|
<optional>true</optional> |
||||
|
<exclusions> |
||||
|
<exclusion> |
||||
|
<groupId>com.vaadin.external.google</groupId> |
||||
|
<artifactId>android-json</artifactId> |
||||
|
</exclusion> |
||||
|
</exclusions> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<finalName>${project.artifactId}</finalName> |
||||
|
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,42 @@ |
|||||
|
package com.elink.esua.epdc.commons.api.version; |
||||
|
|
||||
|
import org.springframework.core.annotation.AnnotationUtils; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.servlet.mvc.condition.RequestCondition; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
||||
|
|
||||
|
import java.lang.reflect.Method; |
||||
|
|
||||
|
/** |
||||
|
* @author rongchao |
||||
|
* @Date 18-11-24 |
||||
|
*/ |
||||
|
public class ApiRequestMappingHandlerMapping extends RequestMappingHandlerMapping { |
||||
|
@Override |
||||
|
protected RequestCondition<?> getCustomMethodCondition(Method method) { |
||||
|
return createCondition(method.getClass()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) { |
||||
|
return createCondition(handlerType); |
||||
|
} |
||||
|
|
||||
|
private static RequestCondition<ApiVersionCondition> createCondition(Class<?> clazz) { |
||||
|
RequestMapping classRequestMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class); |
||||
|
if (classRequestMapping == null) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder mappingUrlBuilder = new StringBuilder(); |
||||
|
if (classRequestMapping.value().length > 0) { |
||||
|
mappingUrlBuilder.append(classRequestMapping.value()[0]); |
||||
|
} |
||||
|
String mappingUrl = mappingUrlBuilder.toString(); |
||||
|
String mappingUrlVersion = "{version}"; |
||||
|
if (!mappingUrl.contains(mappingUrlVersion)) { |
||||
|
return null; |
||||
|
} |
||||
|
ApiVersion apiVersion = AnnotationUtils.findAnnotation(clazz, ApiVersion.class); |
||||
|
return apiVersion == null ? new ApiVersionCondition(1) : new ApiVersionCondition(apiVersion.value()); |
||||
|
} |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package com.elink.esua.epdc.commons.api.version; |
||||
|
|
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
|
||||
|
/** |
||||
|
* @author rongchao |
||||
|
* @Date 18-11-24 |
||||
|
*/ |
||||
|
@Target({ElementType.TYPE}) |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
public @interface ApiVersion { |
||||
|
|
||||
|
/** |
||||
|
* 版本号 |
||||
|
* |
||||
|
* @return |
||||
|
*/ |
||||
|
int value(); |
||||
|
} |
@ -0,0 +1,48 @@ |
|||||
|
package com.elink.esua.epdc.commons.api.version; |
||||
|
|
||||
|
import org.springframework.web.servlet.mvc.condition.RequestCondition; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.util.regex.Matcher; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
/** |
||||
|
* @author rongchao |
||||
|
* @Date 18-11-24 |
||||
|
*/ |
||||
|
public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> { |
||||
|
private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("/v(\\d+).*"); |
||||
|
|
||||
|
private int apiVersion; |
||||
|
|
||||
|
ApiVersionCondition(int apiVersion) { |
||||
|
this.apiVersion = apiVersion; |
||||
|
} |
||||
|
|
||||
|
public int getApiVersion() { |
||||
|
return apiVersion; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public ApiVersionCondition combine(ApiVersionCondition apiVersionCondition) { |
||||
|
return new ApiVersionCondition(apiVersionCondition.getApiVersion()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public ApiVersionCondition getMatchingCondition(HttpServletRequest httpServletRequest) { |
||||
|
Matcher m = VERSION_PREFIX_PATTERN.matcher(httpServletRequest.getRequestURI()); |
||||
|
if (m.find()) { |
||||
|
Integer version = Integer.valueOf(m.group(1)); |
||||
|
if (version >= this.apiVersion) { |
||||
|
return this; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int compareTo(ApiVersionCondition apiVersionCondition, HttpServletRequest httpServletRequest) { |
||||
|
return apiVersionCondition.getApiVersion() - this.apiVersion; |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package com.elink.esua.epdc.commons.api.version.config; |
||||
|
|
||||
|
import com.elink.esua.epdc.commons.api.version.ApiRequestMappingHandlerMapping; |
||||
|
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
||||
|
|
||||
|
/** |
||||
|
* @author rongchao |
||||
|
* @Date 18-11-24 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class WebMvcRegistrationsConfig implements WebMvcRegistrations { |
||||
|
|
||||
|
@Override |
||||
|
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { |
||||
|
return new ApiRequestMappingHandlerMapping(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.elink.esua.epdc.rest.v2; |
||||
|
|
||||
|
import com.elink.esua.epdc.commons.api.version.ApiVersion; |
||||
|
import com.elink.esua.epdc.commons.tools.constant.Constant; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* @author yujintao |
||||
|
* @email yujintao@elink-cn.com |
||||
|
* @date 2019/9/2 14:29 |
||||
|
*/ |
||||
|
@ApiVersion(2) |
||||
|
@RestController("DemoControllerV2") |
||||
|
@RequestMapping("/demo" + Constant.VERSION_CONTROL) |
||||
|
public class DemoV2Controller { |
||||
|
|
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.elink.esua.epdc.entity; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* |
||||
|
* @author yujintao |
||||
|
* @email yujintao@elink-cn.com |
||||
|
* @date 2019/9/2 13:54 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class TransforDemoEntity { |
||||
|
|
||||
|
private Long demoId; |
||||
|
|
||||
|
private String demoName; |
||||
|
|
||||
|
private int demoAge; |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.elink.esua.epdc.transfor; |
||||
|
|
||||
|
import com.elink.esua.epdc.entity.DemoEntity; |
||||
|
import com.elink.esua.epdc.entity.TransforDemoEntity; |
||||
|
import ma.glasnost.orika.MapperFactory; |
||||
|
import net.rakugakibox.spring.boot.orika.OrikaMapperFactoryConfigurer; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* @author yujintao |
||||
|
* @email yujintao@elink-cn.com |
||||
|
* @date 2019/9/2 11:21 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class Demo2DemoTransforEntity implements OrikaMapperFactoryConfigurer { |
||||
|
|
||||
|
@Override |
||||
|
public void configure(MapperFactory orikaMapperFactory) { |
||||
|
orikaMapperFactory.classMap(DemoEntity.class, TransforDemoEntity.class) |
||||
|
.field("id", "demoId") |
||||
|
.field("name", "demoName") |
||||
|
.field("age", "demoAge") |
||||
|
.byDefault() |
||||
|
.register(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,38 @@ |
|||||
|
package com.elink.esua.epdc; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.elink.esua.epdc.entity.DemoEntity; |
||||
|
import com.elink.esua.epdc.entity.TransforDemoEntity; |
||||
|
import ma.glasnost.orika.MapperFacade; |
||||
|
import org.junit.Test; |
||||
|
import org.junit.runner.RunWith; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
|
||||
|
/** |
||||
|
* 实体映射测试类 |
||||
|
* |
||||
|
* @author yujintao |
||||
|
* @email yujintao@elink-cn.com |
||||
|
* @date 2019/9/2 13:25 |
||||
|
*/ |
||||
|
@RunWith(SpringRunner.class) |
||||
|
@SpringBootTest |
||||
|
public class OrikaTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private MapperFacade orikaMapper; |
||||
|
|
||||
|
@Test |
||||
|
public void Test() { |
||||
|
DemoEntity demoEntity = new DemoEntity(); |
||||
|
demoEntity.setId(1000L); |
||||
|
demoEntity.setName("name"); |
||||
|
demoEntity.setAge(19); |
||||
|
|
||||
|
TransforDemoEntity transforEntity = orikaMapper.map(demoEntity, TransforDemoEntity.class); |
||||
|
System.out.println(JSON.toJSONString(transforEntity)); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue