springfox / springfox

Automated JSON API documentation for API's built with Spring
http://springfox.io
Apache License 2.0
5.93k stars 1.54k forks source link

add support to prefix all swagger http requests/resources #2573

Open imod opened 6 years ago

imod commented 6 years ago

What kind of issue is this?

we have the requirement to put all swagger documentation beneath a fix path. This first sounds close to an answer already given in How does one configure swagger-ui for non-springboot applications?. But the answer given there is to add some redirects to make it all work, but this is not going to work when you really only want to give explicit permission for one specific path e.g. /documentation/**. we don't care about the stuff after the path prefix, but it must all be beneath it - no redirects outside this path.

Its the same reasoning why spring boot has management.endpoints.web.base-path=/ to move all endpoints to a subpath. Actuator Web Endpoint Paths

I have done a little experiment and have this:

@SpringBootApplication
@EnableSwagger2
public class SpringfoxSwaggerApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(SpringfoxSwaggerApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/documentation/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/documentation/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Controller
    @ApiIgnore
    @RequestMapping("/documentation/swagger-resources")
    public static class MyApiResourceController extends ApiResourceController {
        public MyApiResourceController(SwaggerResourcesProvider swaggerResources) {
            super(swaggerResources);
            // how to set securityConfiguration and uiConfiguration? both are
            // @Autowired by field injection :(
        }
    }

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("myapi").select().build();
    }

    // just a dummy controller
    @RestController
    @RequestMapping("/api")
    public class MyController {

        @GetMapping
        public String home() {
            return "hello world";
        }
    }
}

This allows me to redefine the RequestMapping for ApiResourceController. The startup works fine, and the UI stuff can also be loaded - but unfortunate these requests fail and I have no idea how to get them to work.

Also setting springfox.documentation.swagger.v2.path=/documentation/v2/api-docs does not help, it makes it even worse, as the it now tries to access http://localhost:8080/documentation/documentation/v2/api-docs?group=myapi

dilipkrish commented 6 years ago

Do you have a repo you can share. This seems like a common enough requirement.

imod commented 6 years ago

@dilipkrish here it is: https://github.com/imod/SpringfoxSwagger-ContextPath

please let me know if there is anything I can help, it is really important for us...

dilipkrish commented 6 years ago

@imod thank you, will research and get back to you

imod commented 6 years ago

@dilipkrish do you get an idea how to enable this? we still struggle with this every day :(

dilipkrish commented 6 years ago

@imod sorry been a little busy with my day job. I'll look at this shortly

imod commented 6 years ago

@dilipkrish no worries - just wanted to know if you have any issues. really appreciate your work!

imod commented 5 years ago

@dilipkrish just to let you know, this is still an issue we would very like to fix - do you have a pointer so I could take a look at it myself again?