Swagger配置 - 访问swagger.json

时间:2015-08-20 10:12:30

标签: java json rest maven swagger

我是一个狂热的人,并希望在我最新的REST API项目上设置它,但是我的失败很糟糕。一切似乎都配置正确但我无法导航到swagger.json而没有得到404.我看不出有什么遗漏或错误

的pom.xml

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

        <exclusions>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>com.sun.jersey</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jersey-multipart</artifactId>
                <groupId>com.sun.jersey.contribs</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.11.1</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.11.1</version>
    </dependency>

项目使用版本1.11.1时排除,如果从swagger导入1.13,则会导致冲突

的web.xml

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            io.swagger.jaxrs.json,
            io.swagger.jaxrs.listing,
            uk.package.test.myproject
        </param-value>
    </init-param>       
    <load-on-startup>1</load-on-startup>
</servlet>

<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:8082/myproject/api</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

示例注释......

@ManagedBean
@Path("secure/cache")
@Restrict("ManageCache")
@Api(value = "secure/cache", description = "Rest api for do operations on     admin", produces = MediaType.APPLICATION_JSON)
public class AdminApi {....


@GET
@Path("{name}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Get specific admin", httpMethod = "GET", notes = "Fetch the admin user details", response = Response.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Given admin user found"),
    @ApiResponse(code = 404, message = "Given admin user not found"),
    @ApiResponse(code = 500, message = "Internal server error due to encoding the data"),
    @ApiResponse(code = 400, message = "Bad request due to decoding the data"),
    @ApiResponse(code = 412, message = "Pre condition failed due to required data not found") })

public List<String> getCacheKeys(.....

项目运行时,会记录以下内容......

10:46:48,724 INFO  [com.sun.jersey.api.core.PackagesResourceConfig] (MSC service thread 1-5) Scanning for root resource and provider classes in the packages:
  io.swagger.jaxrs.json
  io.swagger.jaxrs.listing
  uk.package.test.myproject
10:46:48,771 INFO  [com.sun.jersey.api.core.ScanningResourceConfig] (MSC service thread 1-5) Root resource classes found:
  class uk.package.test.myproject.api.NotificationApi
  class uk.package.test.myproject.api.UserApi
  class uk.package.test.myproject.api.FlagsApi
  class uk.package.test.myproject.api.UnitsByFunctionApi
  class uk.package.test.myproject.api.CvDataApi
  class uk.package.test.myproject.api.ImageApi
  class uk.package.test.myproject.api.GazetteerApi
  class uk.package.test.myproject.api.SearchApi
  class uk.package.test.myproject.api.ViewApi
  class uk.package.test.myproject.api.AdminApi
10:46:48,776 INFO  [com.sun.jersey.api.core.ScanningResourceConfig] (MSC service thread 1-5) No provider classes found.
10:46:48,834 INFO  [com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer] (MSC service thread 1-5) CDI support is enabled
10:46:48,842 INFO  [com.sun.jersey.server.impl.application.WebApplicationImpl] (MSC service thread 1-5) Initiating Jersey application, version 'Jersey: 1.11.1 03/31/2012 06:49 PM'

提前谢谢

干杯

马丁

2 个答案:

答案 0 :(得分:0)

只是为了让您知道我已经通过使用自定义应用程序解决了这个问题,而不是使用包扫描:)

感谢您的帮助

马丁

答案 1 :(得分:0)

您的servlet映射表明您不应该查看指定的路径:

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

这通常意味着要查看/ api路径。那意味着,除非你使用名为myproject的webapp,否则你应该在/api/swagger.json下查看