Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger-UI Does not display with SpringBoot and path parameter on root endpoint

I am trying to setup swagger for an existing Spring Boot API project. I have discovered that I have two endpoints that cause the http://url/swagger-ui.html file to not render. If I comment out these two endpoints then it does render correctly.

When the two endpoints exist, the http://url/v2/api-docs json file does render successfully. I can take the json from api-docs and paste it into https://editor.swagger.io/ and the html page renders correctly on that site.

The two endpoints that are causing the issue are on root path, and have only a path parameter in the url. They are each in a controller that has @RequestMapping("/") and they are annotated;

@PutMapping(value = "{vaultTitleId}", produces = MediaType.APPLICATION_JSON_VALUE)

@DeleteMapping(value = "{vaultTitleId}")

These two endpoints work correctly, they just are causing some issue with Swagger rendering the HTML. If I remove them, the HTML displays. I have tried moving them into a controller by themselves and seeing if I could prevent swagger from accessing them in the Swagger configuration. But, it seems they only have to exist somewhere where spring boot sees them to prevent the html from displaying.

Any advice is appreciated. I would like to use Swagger, but I am giving up on it for now, and looking at alternative tools.

Gradle

compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.9.2")

Swagger Config

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors
            .basePackage("com.nextgearcapital.mt.controller"))
            .paths(PathSelectors.any())
            .build();
}
}
like image 849
Eric Smith Avatar asked Dec 13 '25 17:12

Eric Smith


1 Answers

I discovered a work around. This was suggested in another forum.

https://github.com/springfox/springfox/issues/631

The problem is you're endpoints are consuming the endpoint mappings, you can create a regular expression that resolves it?

@DeleteMapping(value = "{vaultTitleId:^((?!swagger-ui.html).)*$}") Seems al lil clunky but not sure what the right answer is.

Using a request mapping with a regex to define the path parameter did allow the swagger-ui to display. It is not obvious to me why this works. I would call it more of a work around than an actual solution. But, this is what I am going to be using.

    @DeleteMapping(value = "{vaultTitleId:^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}", produces = MediaType.APPLICATION_JSON_VALUE)
like image 141
Eric Smith Avatar answered Dec 15 '25 14:12

Eric Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!