Jersey 2 + Swagger returns empty list APIs
Try to add this to your resource class (constructor):
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);
I had the same problem and used sth like below.
I have a bean that contains a list of possible values generated at app start via config. Swagger should show the list, but this does not work.
@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}
I know its not the wanted solution if you use swagger, but you can specify the input/output via the response fields!