【问题标题】:Jersey 2 + Swagger returns empty list APIsJersey 2 + Swagger 返回空列表 API
【发布时间】:2016-12-26 07:06:07
【问题描述】:

我们使用的是 spring 4.x 和 swagger-jersey2-jaxrs_2.10。 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-docs 返回

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

【问题讨论】:

    标签: java spring swagger jersey-2.0


    【解决方案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
    }
    

    如果您使用 swagger,我知道这不是您想要的解决方案,但您可以通过响应字段指定输入/输出!

    【讨论】:

    • 这段代码对我没有帮助..请提供正确的解决方案来解决我的问题..
    【解决方案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);
    

    【讨论】:

    • 我已经配置了上面的beanConfig..仍然看不到swagger UI...请为spring 4.x和swagger-jersey2-jars_2.10提供swagger配置
    • 我有我描述的配置和招摇的作品。请确保您编写了正确的资源包、基本路径(在您的项目中查看 @ApplicationPath),并且所有端点都必须具有注释 @Api("somepath") 才能在 swagger.json 中。你还在得到空的 swagger.json 吗?
    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2019-01-06
    • 2017-10-26
    相关资源
    最近更新 更多