I found better way to support api versioning. Below is the explanation:
1) Create an annotation: ApiVersion:
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping("/{apiVersion}")
public @interface ApiVersion {
/**
* version
*
* @return
*/
int value() default 1;
boolean latest() default false;
}
2) Create custom RequestCondition to put the matching logic:**
public class ApiVersionCondition implements RequestCondition {
private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("/v(\\d+)/");
private final static Pattern LATEST_PREFIX_PATTERN = Pattern.compile("/latest/");
private int apiVersion;
private boolean latest;
public ApiVersionCondition(int apiVersion, boolean latest) {
this.apiVersion = apiVersion;
this.latest = latest;
}
/**
* Latest definition will take effect, that means method definition will override the classes definition
*
* @param otherApiVersionCondition other condition that is matching.
* @return latest definition of matching condition.
*/
public ApiVersionCondition combine(ApiVersionCondition otherApiVersionCondition) {
return new ApiVersionCondition(otherApiVersionCondition.getApiVersion(), otherApiVersionCondition.isLatest());
}
/**
* Define matcher to match the pattern for api versioning.
* When version number requested is equal to or greater than configured, condition will be returned.
*
* @param request Request instance
* @return ApiVersionCondition based on match
*/
public ApiVersionCondition getMatchingCondition(HttpServletRequest request) {
Matcher m = LATEST_PREFIX_PATTERN.matcher(request.getRequestURI());
if (m.find() && isLatest()) {
return this;
}
m = VERSION_PREFIX_PATTERN.matcher(request.getRequestURI());
if (m.find()) {
Integer version = Integer.valueOf(m.group(1));
if (version >= this.apiVersion)
return this;
}
return null;
}
/**
* When more than one configured version number passes the match rule, the bigest one will take effect.
*
* @param otherApiVersionCondition other api version condition that was satisfied.
* @param request Request instance
* @return 1 if other version condition has greater api version, -1 if this condition has greater api version, 0 if they are same.
*/
public int compareTo(ApiVersionCondition otherApiVersionCondition, HttpServletRequest request) {
return otherApiVersionCondition.getApiVersion() - this.apiVersion;
}
/**
* Getter for api version.
*
* @return api version for the condition.
*/
private int getApiVersion() {
return apiVersion;
}
/**
* Getter for whether latest is configured in annotation.
*
* @return true if latest is configured in annotation, else false.
*/
private boolean isLatest() {
return latest;
}
}
3) Create custom RequestMappingHandlerMapping:
public class ApiVersioningRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) {
return createCondition(AnnotationUtils.findAnnotation(handlerType, ApiVersion.class));
}
@Override
protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) {
return createCondition(AnnotationUtils.findAnnotation(method, ApiVersion.class));
}
private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) {
return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value(), apiVersion.latest());
}
}
4) Ask Spring to use the custom RequestMappingHandlerMapping:
@Configuration
public class WebMvcRegistrationsConfig implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
ApiVersioningRequestMappingHandlerMapping apiVersioningRequestMappingHandlerMapping = new ApiVersioningRequestMappingHandlerMapping();
apiVersioningRequestMappingHandlerMapping.setOrder(0);
apiVersioningRequestMappingHandlerMapping.setRemoveSemicolonContent(false);
return apiVersioningRequestMappingHandlerMapping;
}
}
5) Use it in controller:
@ApiVersion //default is version 1
//@RequestMapping("{apiVersion}/test") //add this if want to specify a common root e.g. v<x>/test for all below request mapping
@RestController
public class GreetingController {
private static final String template = "Hello , %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting") // URI: /v1/greeting will be mapped to this method
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
@ApiVersion(2)
@RequestMapping("/greeting") // URI: /v2/greeting will be mapped to this method
public Greeting greetingV2(@RequestParam(value = "name", defaultValue = "World version 2") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
@ApiVersion(value = 3, latest = true)
@RequestMapping("/greeting") // URI: /v3/greeting OR /latest/greeting will be mapped to this method
public Greeting greetingV3(@RequestParam(value = "name", defaultValue = "World version 3") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
There are still some fine tuning to do but this is a good start for what I wanted to achieve. Will update if find better idea or some improvements. Please let us know if someone finds improvements.