如何为swagger 2.8.0做友好的基本网址

时间:2018-04-19 15:00:31

标签: java rest swagger springfox

我正在尝试更改API文档的基本访问URL。该网址是" http://localhost:8080/swagger-ui.html"。我希望得到类似" http://localhost:8080/myapi/swagger-ui.html"。

的内容

我使用Springfox 2.8.0 Swagger,Java 8,Spring Boot 2.0 招摇的配置是:

CriteriaQuery.join

自定义路径提供程序必须提供帮助,但我仍然可以使用url" http://localhost:8080/swagger-ui.html"来访问api文档。如果我使用网址" http://localhost:8080/myapi/swagger-ui.html",则会收到404错误。请看下面的截图。

enter image description here

5 个答案:

答案 0 :(得分:3)

对于那些使用 Springfox Swagger 3.0.0 的人

Here's 更改文档基本 url 的工作配置:

springfox:
  documentation:
    swaggerUi:
      baseUrl: /documentation
    openApi:
      v3:
        path: /documentation/v3/api-docs
    swagger:
      v2:
        path: /documentation/v2/api-docs

答案 1 :(得分:1)

https://github.com/springfox/springfox/issues/2250 - 正如他们所说,您可以将重定向配置为您自己的路径

答案 2 :(得分:1)

我也遇到了这个问题,并尝试了许多可能的解决方案,但没有任何帮助。 就我而言,我无法使用任何资源重定向,因为必须通过匹配路径/ api-docs / **在Google云端上像本地一样访问swagger。在我的情况下,在Google Cloud上任何资源重定向都将被拒绝。所有资源也必须从此路径加载

这是我的解决方案:
版本2.9.2的springfox-swagger2和springfox-swagger-ui

@EnableSwagger2
@Configuration
public class SwaggerCommonConfig implements WebMvcConfigurer {
    public static final String PATH = "/api-docs";

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController(PATH, "/");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(PATH + "/**").addResourceLocations("classpath:/META-INF/resources/");
    }
}

并且由于springfox不可能通过另一种方式来做,在我的情况下,我们将创建一个简单的控制器,它将资源请求从我们的自定义路径转换为标准springfox。 (这不是很优雅的部分,但它是:))

@RestController
@RequestMapping(SwaggerGatewayCommonConfig.PATH)
@RequiredArgsConstructor
public class SwaggerController {
    private final RestTemplate restTemplate;
    private final static String V2_API_DOCS = "/v2/api-docs";
    private final static String SWAGGER_RESOURCES_CONFIGURATION_UI = "/swagger-resources/configuration/ui";
    private final static String SWAGGER_RESOURCES_CONFIGURATION_SECURITY = "/swagger-resources/configuration/security";
    private final static String SWAGGER_RESOURCES = "/swagger-resources";
    private final static Pattern pattern = Pattern.compile("http[s]*://([^/]+)", Pattern.CASE_INSENSITIVE);

    @Value("${server.port}")
    private String port;

    @GetMapping(V2_API_DOCS)
    @SuppressWarnings("unchecked")
    public Map<String, Object> getV2ApiDocs(HttpServletRequest request) {
        Matcher matcher = pattern.matcher(request.getRequestURL().toString());
        matcher.find();

        Map<String, Object> resp = (Map<String, Object>) restTemplate.getForObject(toLocalSwaggerUrl(V2_API_DOCS), Map.class);
        //we have to replace standard host, to requested host. as swagger UI make api requests from this host
        resp.put("host", matcher.group(1));

        return resp;
    }

    @GetMapping(SWAGGER_RESOURCES_CONFIGURATION_UI)
    public Object getSwaggerResourcesConfigurationUi() {
        return restTemplate.getForObject(toLocalSwaggerUrl(SWAGGER_RESOURCES_CONFIGURATION_UI), Object.class);
    }

    @GetMapping(SWAGGER_RESOURCES_CONFIGURATION_SECURITY)
    public Object getSwaggerResourcesConfigurationSecurity() {
        return restTemplate.getForObject(toLocalSwaggerUrl(SWAGGER_RESOURCES_CONFIGURATION_SECURITY), Object.class);
    }

    @GetMapping(SWAGGER_RESOURCES)
    public Object getSwaggerResources() {
        return restTemplate.getForObject(toLocalSwaggerUrl(SWAGGER_RESOURCES), Object.class);
    }

    private String toLocalSwaggerUrl(String path) {
        return "http://localhost:" + port + path;
    }
}

我希望这也可以节省面对它的人的时间=) 祝你好运

答案 3 :(得分:0)

Swagger基本访问URL是从您的基本应用程序路径构建的,因此,如果您更改基本应用程序路径,您将获得所需的行为,而且所有api都将更改为该路径。您可以在How to set base url for rest in spring boot?处找到更改方法。

您所做的只是改变了您迅速从应用程序中调用其他api的方式,而不更改其基本URL。有一些技巧可以更改swagger基本URL,而无需更改应用程序基本路径(手动移动所有swagger资源),但是我不建议这样做。

答案 4 :(得分:0)

您可以像这样编辑 SwaggerConfiguration

请小心更换package(必须是host 包含您的REST控制器),PATH和所需的@Configuration @EnableSwagger2 public class SwaggerConfiguration implements WebMvcConfigurer { public static final String PATH = "/myapi"; @Bean public Docket api() { final var package = "com.julia.rest"; final var host = "localhost:8080"; return new Docket(DocumentationType.SWAGGER_2) .host(host) .select() .apis(RequestHandlerSelectors.basePackage(package)) .paths(PathSelectors.any()) .build(); } @Override public void addViewControllers(ViewControllerRegistry registry) { final var apiDocs = "/v2/api-docs"; final var configUi = "/swagger-resources/configuration/ui"; final var configSecurity = "/swagger-resources/configuration/security"; final var resources = "/swagger-resources"; registry.addRedirectViewController(PATH + apiDocs, apiDocs).setKeepQueryParams(true); registry.addRedirectViewController(PATH + resources, resources); registry.addRedirectViewController(PATH + configUi, configUi); registry.addRedirectViewController(PATH + configSecurity, configSecurity); registry.addRedirectViewController(PATH, "/"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(PATH + "/**").addResourceLocations("classpath:/META-INF/resources/"); } }

application.properties

另一种解决方案是通过更改spring-boot URL 上下文路径

编辑倒server.servlet.context-path=/myapi 文件:

application.yml

或者如果您有一个server: servlet: context-path: /myapi 文件:

public class ButtonTagHelper: TagHelper  {
    public string BsButtonColor { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.Attributes.SetAttribute("class", $"btn btn-{BsButtonColor}");
    }
}

警告:它将更改您所有Web服务的基本路径,而不仅仅是Swagger