更改位置以在Spring中调用swagger-ui

时间:2017-09-12 15:30:39

标签: spring swagger springfox

如何更改位置以将swagger api文档从http://localhost:8081/my-api-doc调用到@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.package")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); }

我的SwaggerConfig是

springfox-swagger2

我将springfox-swagger-uiversion 2.7.0LocationStrategy一起使用。

2 个答案:

答案 0 :(得分:0)

这就是我能做到的一切。

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

            registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs?group=restful-api");
            registry.addRedirectViewController("/documentation/swagger-resources/configuration/ui","/swagger-resources/configuration/ui");
            registry.addRedirectViewController("/documentation/swagger-resources/configuration/security","/swagger-resources/configuration/security");
            registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources");
            registry.addRedirectViewController("/documentation", "/documentation/swagger-ui.html");
    }


    @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/");
    }

此方法添加在一个扩展WebMvcConfigurerAdapter的配置类中 public class Configuration extends WebMvcConfigurerAdapter {...}

您无法删除swagger-ui.html或更改,因为源代码中已对其进行了硬编码。

答案 1 :(得分:0)

如果使用Spring Boot,则可以添加一个映射来重定向请求,例如:

@GetMapping("/swagger")
public void swaggerRedirect(HttpServletResponse response) {
    response.setHeader("Location", "/swagger-ui.html");
    response.setStatus(302);
}