Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Rest API version header in SwaggerUI

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?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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;
}
}
about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!