Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 646
Alex Avatar asked Oct 29 '22 23:10

Alex


1 Answers

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

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
like image 72
Dilip Krishnan Avatar answered Nov 15 '22 04:11

Dilip Krishnan