I want to use Springfox SwaggerUI for my Rest API (spring-mvc) documentation. I use version header in @RequestMapping annotation, but if I have two versions of same method, in SwaggerUI I can see only one.
For example:
@GetMapping(value = "/users", headers = "X-API-VERSION=1")
public List<User> getUsersV1(){...}
@GetMapping(value = "/users", headers = "X-API-VERSION=2")
public List<User> getUsersV2(){...}
Above code results in only one method visible in api documentation.
Is there any option to configure Swagger to differ endpoints with consideration of my version header?
After some research I have found solution to my problem, maybe it will help someone in the future. I add "#v" suffix to path using PathDecorator.
Now I can see all my methods in generated documentation.
@Component
@Order(value = Ordered.HIGHEST_PRECEDENCE + 70)
public class VersionPathDecorator implements PathDecorator {
private final static Logger logger = LoggerFactory.getLogger(VersionPathDecorator.class);
@Override
public Function<String, String> decorator(PathContext context) {
return (path) -> {
StringBuilder sb = new StringBuilder(path);
Field parent = null;
try {
parent = PathContext.class.getDeclaredField("parent");
parent.setAccessible(true);
RequestMappingContext rmc = (RequestMappingContext) parent.get(context);
rmc.headers()
.stream()
.filter(h -> RequestHeader.X_API_VERSION.headerName.equals(h.getName()))
.map(NameValueExpression::getValue)
.findFirst()
.ifPresent(v -> sb.append("#v").append(v));
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.error("path decoration failed", e);
}
return sb.toString();
};
}
@Override
public boolean supports(DocumentationContext documentationContext) {
return true;
}
}
Swagger identifies services by its endpoint.
Each feature must respond to a different endpoint, and headers for that function should not be used.
If you are using REST services read a bit about Restfull and follow its principles. This url can help you: http://docs.oracle.com/javaee/6/tutorial/doc/gijqy.html