2016-08-19 85 views
8

我們使用spring 4.x和swagger-jersey2-jaxrs_2.10。 Swagger沒有列出我的API,它總是隻返回版本信息。Jersey 2 + Swagger返回空列表API

的pom.xml

<dependency> 
    <groupId>com.wordnik</groupId> 
    <artifactId>swagger-jersey2-jaxrs_2.10</artifactId> 
    <version>1.3.13</version> 
    <scope>compile</scope> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.core</groupId> 
    <artifactId>jersey-server</artifactId> 
    <version>2.23.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.containers</groupId> 
    <artifactId>jersey-container-servlet-core</artifactId> 
    <version>2.23.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-multipart</artifactId> 
    <version>2.23.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-json-jackson</artifactId> 
    <version>2.23.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.jersey.ext</groupId> 
    <artifactId>jersey-spring3</artifactId> 
    <version>2.23.2</version> 
    <exclusions> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
     </exclusion> 
     <exclusion> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
     </exclusion> 
     <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-aop</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 

的web.xml

<filter> 
     <filter-name>SpringApplication</filter-name> 
     <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class> 

     <init-param> 
      <param-name>javax.ws.rs.Application</param-name> 
      <param-value>xxx.xxx.xxx.filter.JerseyApiSpringFilter</param-value> 
     </init-param> 

     <init-param> 
      <param-name>jersey.config.servlet.filter.forwardOn404</param-name> 
      <param-value>false</param-value> 
     </init-param> 

     <init-param> 
      <param-name>jersey.config.servlet.filter.staticContentRegex</param-name> 
      <param-value>/docs/.*</param-value> 
     </init-param> 
    </filter> 

資源類

@Path("/listApi") 
@Component 
@Scope("request") 
@Api(value = "/listApi", description = "List API") 
@Produces({"application/json"}) 
public class ListApiResource { 

    @GET 
    @Path("/") 
    @Produces(MediaType.APPLICATION_JSON) 
    @ApiOperation(value = "Retrieve all list of apps", response = listDto.class) 
    public Response getAllApps (@QueryParam("appId") int appId) { 

      // code 
    } 
} 

ResourceConfig類

public class JerseyApiSpringFilter extends ResourceConfig { 
     static { 
    //  JaxrsApiReader.setFormatString(""); 
     } 

     public JerseyApiSpringFilter() { 

      packages("com.xxx.xxxx.xxxxxx.resources"); 
      register(RequestContextFilter.class); 
      register(ApiListingResource.class); 
      register(ApiListingResourceJSON.class); 
      register(JerseyApiDeclarationProvider.class); 
      register(JerseyResourceListingProvider.class); 
      register(MultiPartFeature.class); 
      register(JacksonFeature.class); 
     } 

我的應用程序basebath/API的文檔返回

{ 
    "apiVersion": "1.0.0", 
    "swaggerVersion": "1.2", 
} 

回答

1

我有同樣的問題,使用某物像下面。

我有一個bean,其中包含應用程序啓動時通過配置生成的可能值列表。 Swagger應該顯示列表,但這不起作用。

@GET 
@Path("info/types") 
@PermitAll 
@Produces(MediaType.APPLICATION_JSON) 
@ApiOperation(value = "Possible bean types", notes = "List all available bean types", 
     response = BeanTypesList.class, responseContainer = "List") 
@ApiResponses(value = { @ApiResponse(code = 200, message = "List of all available bean types", 
     response = BeanTypesList.class) }) 
@Timed 
@ExceptionMetered 
@CacheControl(maxAge = 6, maxAgeUnit = TimeUnit.HOURS) 
public List<BeanType> getBeanTypes() throws JsonProcessingException { 
    return new ArrayList<BeanType>(BeanType.values()); 
} 

public class SwaggerClassHelper { 

    @ApiModel(value = "BeanTypesList", description = "Overview of possible bean types") 
    public class BeanTypesList { 

     @ApiModelProperty(value = "List of several possible bean types.", required = true) 
     private List<BeanType> types; 

     @JsonCreator 
     public BeanTypesList(
       List<BeanType> types) { 
      this.types = types; 
     } 

     public List<BeanType> getTypes() { 
      return this.types; 
     } 

     @JsonIgnore 
     @Override 
     public String toString() { 
      return ReflectionToStringBuilder.toString(this); 
     } 
    } 
} 

@ApiModel(value = "Bean type", description = "Represents the type of a bean.") 
public final class BeanType { 

    @JsonIgnore 
    private static Set<String> names = new HashSet<String>(); 

    @JsonProperty("name") 
    private String name; 

    private BeanType(
      List<String> names) { 
     synchronized (BeanType.names) { 
      BeanType.names.addAll(names); 
     } 
    } 

    private BeanType(
      String name) { 
     this.name = name; 
    } 

    //... other methods 
} 

我知道它不是想要的解決方案,如果您使用招搖,但你可以通過響應字段中指定的輸入/輸出!

+0

此代碼是沒有幫助我..請提供一個合適的解決方案,以解決我的問題.. – SST

2

嘗試將此添加到您的資源類(構造函數):

register(ApiListingResource.class); 
    register(SwaggerSerializers.class); 

    BeanConfig beanConfig = new BeanConfig(); 
    beanConfig.setVersion("1.0.0"); 
    beanConfig.setSchemes(new String[]{"http"}); 
    beanConfig.setBasePath("/api"); //or insert your base path (see main Jersey app class) 
    beanConfig.setResourcePackage("com.xxx.where.your.endpoints"); 
    beanConfig.setScan(true); 
    beanConfig.setPrettyPrint(true); 
+0

我與上述beanConfig配置。 。Stil我無法看到swagger UI ...請爲spring 4.x和swagger-jersey2-jars_2.10 – SST

+0

提供swagger配置我已經配置了我描述的和swagger的作品。請確保你編寫了正確的資源包,basepath(在你的項目中查看@ApplicationPath),所有的endpoins必須有註解@Api(「somepath」)在swagger.json中。你還在空蕩蕩的swagger.json? – kosbr