Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda