swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

BeanConfig is not setting up swagger instance correctly and is missing properties

Open tomas-silhavy opened this issue 9 years ago • 1 comments

Cheers, I have application based on Jersey and Spring. I had few problems setting up Swagger and while debugging I found some issues. I have just @Path annotations over service classes. Webapp context root is /fielders. I hope I have setup everything correctly, if not please correct me.

pom.xml

swagger-jersey2-jaxrs: 1.5.13
jersey-container-servlet: 2.25.1
jersey-spring4: 4.3.8.RELEASE
spring-context: 4.3.8.RELEASE
spring-web: 4.3.8.RELEASE

My setup:

web.xml:

   ...
    <servlet>
        <servlet-name>JerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>nz.co.prodserver.configuration.JerseyConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
   ...

JerseyConfig.java

@Component
public class JerseyConfig  extends ResourceConfig {

    public JerseyConfig() {
        packages("nz.co.prodserver.rest", "io.swagger.jaxrs.listing");
    }
}

applicationContext.xml

    <bean name="swaggerConfig" class="nz.co.prodserver.configuration.SwaggerBeanConfig">
        <property name="schemes" value="http" />
        <!--<property name="filterClass" value="io.swagger.sample.util.ApiAuthorizationFilterImpl"/>-->
        <property name="scanAllResources" value="true" />
        <property name="resourcePackage" value="nz.co.prodserver.rest"/>
        <property name="version" value="1.0.0"/>
        <property name="host" value="localhost:8080"/>
        <property name="basePath" value="/fielders/rest"/>
        <property name="title" value="Fielders"/>
        <property name="description" value="Fielders android API"/>
        <property name="contact" value="[email protected]"/>
        <property name="prettyPrint" value="true"/>
        <property name="scan" value="true"/>
    </bean>

First of all scanAllResources is not possible to set out of the box, I needed to extend BeanConfig and expose the setter with some ugly reflection (package only access of reader field). Then when I access localhost:8080/fileders/rest/swagger.json I get only general info and nothing about any rest service running. While debugging I found out that SwaggerConfigLocator has no reference to configured swagger. I fixed this by overriding setScan method like this:

    @Override
    public void setScan(boolean shouldScan) {
        scanAndRead();
        new SwaggerContextService()
                .withConfigId(getConfigId())
                .withScannerId(getScannerId())
                .withContextId(getContextId())
//                .withServletConfig(servletConfig) // I don't have access to servletConfig again, but it's null anyway
                .withSwaggerConfig(this)
                .withScanner(this)
                .withBasePath(getBasePath())
                .withPathBasedConfig(isUsePathBasedConfig())
                .initConfig(this.getSwagger()) //adding this.getSwagger() here instead of null fixes the problem
                .initScanner();
    }

After this change I am getting full swagger.json/yaml as expected, although for some reason it's not prettyPrinted even when I set it to be :/ It would be good to make properties either protected or at least create setters and getters for them (especially scanAllResources).

Side note: there was an exception when I had setter for some another autowired bean inside my Rest service, bcs swagger was trying by reflection to add some annotation that setter parameter...wierd

tomas-silhavy avatar May 31 '17 05:05 tomas-silhavy

Closing this issue as part of our backlog cleanup effort.
This issue is outdated and/or inactive for a long time, and we’re not able to revisit it meaningfully.

If this problem is still relevant, please open a new issue and reference this one for context.
Thank you for your understanding!

MichakrawSB avatar Mar 08 '26 19:03 MichakrawSB