I am following the upgrade guide for 1.3.12 to 1.5.6 along with the Jersey 2.X Project Setup guide. I'm running into a problem with the 'Custom Application' subclass, which provides the sample code below:
public class SampleApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
//resources.add(FirstResource.class);
//resources.add(SecondResource.class);
//...
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
}
Our project deviates from the example you provide because it extends a Jersey ResourceConfig
class, rather than Application
class directly. The problem is that the ResourceConfig
class itself overrides the getClasses()
method and declares it final.
Are you aware of any way to work around this restriction, either by adding the necessary resources to some other part of the jersey application or through some other means of coaxing swagger? Adding them via the ResourceConfig.register()
helper method does make the swagger.json
file available at the usual path, but does not populate it with the documentation for the endpoint classes that have been annotated with @Api
and are also registered()
(and used to work before upgrading). I don't want to go down the route of the web.xml servlet or filter.
The generated swagger.json just contains the extra stuff I defined in the new BeanConfig, like this:
// http://localhost:8080/v1.0/swagger.json
{
"swagger": "2.0",
"info": {
"description": "Description of my App.",
"version": "1.0",
"title": "MyApp"
},
"basePath": "/v1.0"
}
Thanks for your help.
As long as you add the resources, it's fine. Using ResourceConfig.register()
is a fine solution. You're not seeing it being populated, because you haven't completed the final step in guide - https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#3-configure-and-initialize-swagger.
The final step you're referring to, is that the getClasses()
that I mention? Or the BeanConfig
bits? For the BeanConfig
I have the following:
@PostConstruct
/**
* Initializes Swagger Configuration
*/
public void initializeSwaggerConfiguration() {
final ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
scanner.setResourcePackage("com.my.project.api");
ScannerFactory.setScanner(scanner);
BeanConfig config = new BeanConfig();
config.setTitle("MyApp");
config.setDescription("Description of my App");
config.setVersion("1.0");
config.setBasePath("/v1.0");
config.setScan(true);
}
The only thing I was missing from the example was the setResourcePackage
– I'll try that tomorrow. The host and schema bits are option right? I remember reading that somewhere...
host
and schema
are optional, yeah.
Without setResroucePackage
, you'll get an empty output yeah, just as you can see ;)
Most helpful comment
host
andschema
are optional, yeah.Without
setResroucePackage
, you'll get an empty output yeah, just as you can see ;)