在Jersey + Spring + Tomcat环境中不生成swagger.json

时间:2016-05-24 15:57:45

标签: java api tomcat jersey swagger

我已经阅读了Swagger文档,并根据文档对其进行了配置。

当我请求http://localhost:8080/rest/api-docs页面时,Swagger会正确显示所有信息,但不会生成swagger.json,因此http://localhost:8080/rest/swagger.json会返回404。

我的端点http://localhost:8080/rest/test也可以。

这是我的web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.test.JerseyConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<!-- Swagger Config -->
<servlet>
    <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</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/rest/</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

JerseyConfig上课:

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(Test.class);
        //....
        // Swagger
        register(com.wordnik.swagger.jersey.listing.ApiListingResource.class);
        register(com.wordnik.swagger.jersey.listing.ApiListingResourceJSON.class);
        register(com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider.class);
        register(com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider.class);
    }
}

Test.class是一个简单的终点,用Swagger @Api注释。

@Path("/test")
@Api(value = "/test", description = "tttttt")
public class TestResource {

   @GET
   @ApiOperation(value = "Checks server")
   @ApiResponses(@ApiResponse(code = 200, message = "Server is available"))
   public Response check() {
       return Response.ok().build();
   }
}

App部署在Tomcat中。

根据Jersey ResourceModel的说法,Swagger创建的唯一端点是:

   GET     /api-docs
   GET     /api-docs/{route: .+}

1 个答案:

答案 0 :(得分:0)

swagger.json位置是swagger定义的推荐位置,但它不是唯一的位置。请尝试将api-docs位置放在你的swagger-ui中,它应该可以正常工作。