Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda