简体   繁体   中英

Swagger implementation for exiting RESTAPI

I am trying to implement Swagger API to generate API documentation for my Rest API. I have searched swagger.io website for getting started on Swagger configuration, however, failed to generate API.

Here is my dependency in pom.xml

<dependency>
     <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jaxrs</artifactId>
        <version>1.5.0</version>
</dependency>

and bean class in applicationContext.xml:

<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
    <property name="title" value="Swagger Sample App"/>
    <property name="version" value="1.0.0" />
    <property name="schemes" value="http" />
    <property name="host" value="localhost:8002" />
    <property name="basePath" value="/api"/>
    <property name="resourcePackage" value="io.swagger.resources"/>
    <property name="scan" value="true"/>
</bean>

and my web.xml

<servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/api</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
</servlet>

Here, in web.xml, I am getting error at Jersey2Config as it is looking for a servlet mapping. In Swagger.io, it was mentioned that no servlet mapping is required. Can anybody point to me right direction to create Swagger API? Thanks.

If you're using the DefaultJaxrsConfig, you don't need to use the BeanConfig. That's used for programmatic configuration without the web.xml.

You can get rid of one or the other, but not both. It's probably simplest to use the web.xml, see this example:

https://github.com/swagger-api/swagger-samples/tree/master/java/java-jersey-jaxrs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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