Swagger Springfox 配置问题 [英] Swagger Springfox Configuration Issue

查看:62
本文介绍了Swagger Springfox 配置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Spring MVC 处理 REST 调用的应用程序,控制器是 REST 控制器并用 @RestController 注释,控制器及其方法用 @RequestMapping.

I have an application that uses Spring MVC to handle REST calls, the Controllers are REST Controllers and annotated with @RestController, and the controller and its methods are annotated with @RequestMapping.

我正在尝试添加 Swagger 来为现有的 REST 服务生成文档.我正在尝试将其添加为一个测试,以查看它的外观.我添加了 springfoxui 库,向 spring context xml 文件添加了 swagger 配置类和 bean 定义.我可以点击 swagger-ui.html 链接,但它根本没有生成任何文档.由于文档稀少,我不知道我做错了什么或配置错误,谷歌搜索这个问题并没有产生任何有用的东西.

I'm trying to add Swagger to generate documentation for the existing REST services. I'm trying to add it for one as a test to see what it looks like. I've added the springfox and ui libraries, added swagger config classes and bean definitions to the spring context xml file. I can hit the swagger-ui.html link, but it's not generating any documentation at all. Because of the sparse documentation, I have no idea what I'm doing wrong or misconfigured, and Googling the issue doesn't produce anything useful.

谁能提供一些我可能做错的建议,或者提供一些比我目前找到的更好的文档?

Can anyone provide some suggestions of what I might have done wrong, or alternatively, some better documentation than I've found so far?

我在我的 pom 文件中添加了 springfox-swagger2 和相关的 ui 条目,并添加了以下内容:到我的 spring 文件,以及这些类的两个新 bean 定义:

I added springfox-swagger2 and the associated ui entry to my pom file, added this: to my spring file, along with two new bean definitions for these classes:

 @Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

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

}

@EnableWebMvc
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {

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

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

我已经尝试在我的 spring xml 的第二个类中定义 bean,但这也不起作用.我难住了.我已经完成了文档所说的操作,但它不起作用.有什么想法吗?

I've tried defining the beans in the second class in my spring xml, but that didn't work either. I'm stumped. I've done what the documentation said to do, and it's not working. Any ideas?

推荐答案

springfox 文档可用 这里.从您的帖子看来,您可能需要执行以下操作

The springfox documentation is available here. From your post it appears that you may need to do the following

  • Add a dependency for the swagger ui that comes bundled with springfox. You should this dependency springfox-swagger-ui OR springfox-swagger-ui-rfc6570 to your dependencies.
  • Configure your swagger-ui resources so that it can be loaded by your spring application as shown below
    @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");
    }

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

注意:如果您有 Spring Boot 应用程序,则不需要.

NOTE: this is not needed if you have spring boot application.

  • 确保您使用的是最新的库版本(在撰写本文时为 2.6.0)
  • 验证您可以在 http(s)://your-host/context-path/v2/api-docs
  • 看到 swagger 服务描述
  • Make sure you're using the latest library version (at the time of this writing it is 2.6.0)
  • Verify that you can see the swagger service description at http(s)://your-host/context-path/v2/api-docs

这篇关于Swagger Springfox 配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆