简体   繁体   English

Swagger Springfox配置问题

[英]Swagger Springfox Configuration Issue

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 . 我有一个使用Spring MVC来处理REST调用的应用程序,控制器是REST控制器并使用@RestController进行注释,控制器及其方法使用@RequestMapping进行注释。

I'm trying to add Swagger to generate documentation for the existing REST services. 我正在尝试添加Swagger来生成现有REST服务的文档。 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. 我添加了springfoxui库,在spring context xml文件中添加了swagger配置类和bean定义。 I can hit the swagger-ui.html link, but it's not generating any documentation at all. 我可以点击swagger-ui.html链接,但它根本没有生成任何文档。 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? 任何人都可以提供一些关于我可能做错了什么的建议,或者一些比我迄今为止发现的更好的文档吗?

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: 我将springfox-swagger2和相关的ui条目添加到我的pom文件中,将其添加到我的spring文件中,以及这些类的两个新bean定义:

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

}

I've tried defining the beans in the second class in my spring xml, but that didn't work either. 我已经尝试在我的spring xml中定义第二类中的bean,但这也不起作用。 I'm stumped. 我很难过。 I've done what the documentation said to do, and it's not working. 我已经完成了文档说要做的事情,而且它没有用。 Any ideas? 有任何想法吗?

The springfox documentation is available here . springfox文档可在此处获得 From your post it appears that you may need to do the following 从您的帖子看来,您可能需要执行以下操作

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

NOTE: this is not needed if you have spring boot application. 注意:如果您有弹簧启动应用程序,则不需要这样做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM