Swagger Springfox Configuration Issue

2019-06-21 14:46发布

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.

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?

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

}

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?

1条回答
倾城 Initia
2楼-- · 2019-06-21 15:40

The springfox documentation is available here. 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)
  • Verify that you can see the swagger service description at http(s)://your-host/context-path/v2/api-docs
查看更多
登录 后发表回答