如何访问Swagger文档

时间:2020-08-11 07:50:00

标签: java swagger swagger-ui

我有一个带有REST服务的Java应用程序,我想用Swagger进行记录,因此我正在关注this教程。

我有以下内容:

MyRestApplication.java

@ApplicationPath("rest")
public class MyRestApplication extends Application
{

    /**
     * Set the Swagger access
     */
    public TravellinckRestApplication() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setTitle("NexCT API");
        beanConfig.setSchemes(new String[]{"https"});
        beanConfig.setHost("localhost:8443");
        beanConfig.setBasePath("/corporateInterface/rest");
//        beanConfig.setResourcePackage("io.swagger.resources");
        beanConfig.setResourcePackage("com.nexct");
        beanConfig.setScan(true);
    }

    @Override
    public Set<Class<?>> getClasses()
    {
        Set<Class<?>> resourceClasses = new HashSet<Class<?>>();

        // Resources / Endpoints

        resourceClasses.add(io.swagger.jaxrs.listing.ApiListingResource.class);
        resourceClasses.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

        return  resourceClasses;
    }
}

pom.xml

        <plugin>
            <groupId>com.googlecode.maven-download-plugin</groupId>
            <artifactId>download-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>swagger-ui</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>wget</goal>
                    </goals>
                    <configuration>
                        <skipCache>false</skipCache>
                        <url>https://github.com/swagger-api/swagger-ui/archive/master.tar.gz</url>
                        <unpack>true</unpack>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/${build.finalName}/docs</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.build.directory}/swagger-ui-master/dist</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <version>1.5.3</version>
            <artifactId>replacer</artifactId>
            <executions>
                <execution>
                    <id>replace-swagger-json-location</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <file>${project.build.directory}/corporateInterface/docs/index.html</file>
                <replacements>
                    <replacement>
                        <token>https://petstore.swagger.io/v2/swagger.json</token>
                        <value>/corporateInterface/api/swagger.json</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>

然后我运行:

mvn clean install -DskipTests;

并将生成的EAR文件复制到jboss-as-7.0.2.Final/standalone/deployments文件夹中。

现在我可以访问:

https:// localhost:8443 / corporateInterface / rest / swagger.json

{"swagger":"2.0","info":{"version":"1.0.0","title":"NexCT API"},"host":"localhost:8443","basePath":"/corporateInterface/rest","schemes":["https"]}

问题

如何访问Swagger文档?

例如https://localhost:8443/corporateInterface/rest/docs返回404

0 个答案:

没有答案