Springfox swagger - no api-docs with spring boot jersey and gradle

后端 未结 3 703
-上瘾入骨i
-上瘾入骨i 2021-01-18 20:15

I have a spring boot application with jersey and gradle, and I am trying to automatically generate the API documentation using springfox.

I have followed the steps h

3条回答
  •  灰色年华
    2021-01-18 20:47

    Thanks @Dilip-Krishnan for the springfox update and @Guy-Hudara for the question, I came up with the following solution to get swagger support in my springboot jersey powered app :

    import io.swagger.jaxrs.config.BeanConfig;
    import io.swagger.jaxrs.listing.ApiListingResource;
    import io.swagger.jaxrs.listing.SwaggerSerializers;
    import org.glassfish.jersey.server.ResourceConfig;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.PostConstruct;
    
    /**
     * As of version 2.5.0 springfox only supports spring-mvc controllers. Jax-rs implementations like jersey aren't supported.
     *
     * Fortunately io.swagger::swagger-jersey2-jaxrs::1.5.3 have the hooks needed to implement support for jersey in 2.6+.
     *
     * some pointers I used to get this swagger config done and swagger-core, springboot and jersey integrated:
     * http://stackoverflow.com/questions/37640863/springfox-swagger-no-api-docs-with-spring-boot-jersey-and-gardle
     * https://www.insaneprogramming.be/blog/2015/09/04/spring-jaxrs/
     * https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#adding-the-dependencies-to-your-application
     *
     */
    @Configuration
    public class SwaggerConfiguration {
    
        @Autowired
        ResourceConfig resourceConfig;
    
        @PostConstruct
        public void configure() {
    
            resourceConfig.register(ApiListingResource.class);
            resourceConfig.register(SwaggerSerializers.class);
    
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setVersion("1.0.2");
            beanConfig.setSchemes(new String[]{"http"});
            beanConfig.setHost("localhost:8888");
            beanConfig.setBasePath("/api");
            beanConfig.setResourcePackage("com.my.resource");
            beanConfig.setPrettyPrint(true);
            beanConfig.setScan(true);
    
        }
    }
    

    That worked out great for me

提交回复
热议问题