Custom Application subclass singletons not scanned
I originally posted a question on the Google Group and was directed here.
I am creating bundles for an OSGi platform. One of the bundles contains some REST endpoints, using JAX-RS and CXF. I am using Swagger 1.3.12 with Scala 2.10. I am not using any .xml for configuration, but instead am using beanConfig and a custom Application subclass. The issue comes from my endpoints needing to be added to the custom Application subclass as singletons, instead of classes.
Here is my startup method for the REST Endpoint component.
public void startup() throws Exception
{
patientEndpoint = new PatientEndpointImpl(patientDataProvider, encounterDataProvider);
encounterEndpoint = new EncounterEndpointImpl(encounterDataProvider);
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
JAXRSApplication app = new JAXRSApplication();
app.addClass(ApiListingResource.class);
app.addClass(ApiDeclarationProvider.class);
app.addClass(ApiListingResourceJSON.class);
app.addClass(ResourceListingProvider.class);
//app.addClass(PatientEndpoint.class);
//app.addClass(EncounterEndpoint.class);
app.addSingletons(patientEndpoint, encounterEndpoint);
app.addSingleton(new JacksonJsonProvider(mapper));
app.addSingleton(new AccessControlResponseFilter(true));
service.registerApplication(ALIAS, app);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("0.0.1");
beanConfig.setBasePath(ALIAS);
beanConfig.setResourcePackage("com.lexmark.healthcare.lhcdb.endpoint.rs");
beanConfig.setDescription("Healthcare Database resources");
beanConfig.setTitle("Healthcare Database REST API");
beanConfig.setScan(true);
ScannerFactory.setScanner(new DefaultJaxrsScanner());
//ScannerFactory.setScanner(new ReflectiveJaxrsScanner());
ClassReaders.setReader(new DefaultJaxrsApiReader());
logger.info("PatientLookup has successfully started");
}
JAXRSApplication is the custom Application subclass.
This is the configuration that I would expect to work. Unfortunately, when I add my patientEndpoint and encounterEndpoint to the JAXRSApplication as singletons, my resources do not seem to be scanned. The resulting JSON that I get from my api-docs endpoint does not contain my APIs.
However, if I un-comment out the lines:
app.addClass(PatientEndpoint.class);
app.addClass(EncounterEndpoint.class);
and comment out the line app.addSingletons(patientEndpoint, encounterEndpoint);
then my api-docs endpoint will contain my APIs. But my endpoints are then broken, because they need to be initialized with a provided service.
Also of note, my endpoints contain an interface (e.g. PatientEndpoint) and an implementation class (e.g. PatientEndpointImpl). My interface contains the Swagger Annotations.
The REST component class is kept in the package ending in endpoint.rs', the interfaces are inendpoint.rs.apiand the implementations are inendpoint.rs.impl`.
The DefaultJaxrsScanner class looks like it should be grabbing the list of singletons fro the Application subclass, and getting the respective classes, but it doesn't seem to be working for me.
Are singletons supported currently?
Thanks for any help you may be able to provide.
Just curious, what does the P2 tag mean?
Means we're looking into the issue but if it can't be addressed for the release, it can be bumped to the next one
@cprokopiak - did you ever test it with the 1.5 version?
Please reopen if still an issue.
@webron - I am currently trying to test it with the 1.5 release. However, I have been unable to get swagger to actually scan my resources, either using Singletons or Classes.
Below is how I am currently trying to test my configuration and initialization.
application = new JAXRSApplication();
application.addSingleton(enterprisePatientEndpoint);
application.addSingleton(new JacksonJsonProvider());
if (corsFilter != null) {
application.addSingleton(corsFilter);
}
application.addSingleton(authentication);
application.addClass(ApiListingResource.class);
application.addClass(SwaggerSerializers.class);
service.registerApplication(ALIAS, application);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("0.0.1");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:80");
beanConfig.setBasePath("/rs" + ALIAS);
beanConfig.setDescription("Healthcare Database Resources");
beanConfig.setTitle("Healthcare Database REST API");
beanConfig.setResourcePackage("phcdb.endpoint.rs");
beanConfig.setScan(true);
beanConfig.setPrettyPrint(true);
logger.info("REST endpoints registered successfully");
This generates the following in my swagger.json file:
{
"swagger" : "2.0",
"info" : {
"description" : "Healthcare Database Resources",
"version" : "0.0.1",
"title" : "Healthcare Database REST API"
},
"host" : "localhost:80",
"basePath" : "/rs/phcdb",
"schemes" : [ "http" ]
}
One thing I am unclear on so far, do I need to specify a scanner or reader like I had back in the 1.3.x version? I had tried setting the scanner to DefaultJaxrsScanner, but that seemed to conflict with my BeanConfig, resulting in a swagger.json that looked like this:
{ "swagger" : "2.0" }
I've turned up the logging to the highest level in the platform, as well as tried to set a logback.xml file just for io.swagger, but I haven't seen any debug logging from swagger itself. I'm not sure if I don't have something configured properly for logging, or if nothing in swagger that has log statements is actually running.
Do you use CXF or Restlet? Can't find reference to JAXRSApplication anywhere but in Restlet.
I am using CXF as part of an in-house framework. The framework provides JAXRSApplication:
public class JAXRSApplication extends Application {
private final Set<Class<?>> classes = new HashSet<Class<?>>();
private final Set<Object> singletons = new HashSet<Object>();
public JAXRSApplication() {
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
public void addClass(Class<?> clazz) {
classes.add(clazz);
}
public void addClasses(Class<?>... types) {
addClasses(Arrays.asList(types));
}
public void addClasses(Collection<Class<?>> types) {
for (Class<?> clazz : types) {
addClass(clazz);
}
}
public void addSingleton(Object singleton) {
singletons.add(singleton);
}
public void addSingletons(Object... objects) {
addSingletons(Arrays.asList(objects));
}
public void addSingletons(Collection<Object> objects) {
for (Object obj : objects) {
addSingleton(obj);
}
}
}
To be sure, you're using 1.5.0 (with the io.swagger package), right?
Looking at the original post and the new one, it looks like you're mentioning two different packages. Are your resources find directly under the phcdb.endpoint.rs package (it's a bit of an odd, yet valid, package name)?
Yes, I am now testing with the io.swagger 1.5.0 packages.
We are currently in the process of renaming some of our packages.
Under phcdb.endpoint.rs is the file containing the configuration and initialization of my JAXRSApplication. phcdb.endpoint.rs.api contains the interfaces for the API, and this is where I've applied my swagger annotations. phcdb.endpoint.rs.impl contains the implementations of the API.
I've tried setting the setResourcePackage option to all three packages mentioned, but it didn't seem to make a difference. Which package should I be setting for the resources?
The original question mentioned that this seems to be an issue when adding the singletons to the Application class. After migrating to the latest version of swagger-core, is this still the case? That is, if you comment the addition of the singletons, do you get the proper output or does the problem persist?
With the latest version, I have not been able to get the proper output using either singletons or classes.
Okay, this is odd. The next steps I can think of is trying to turn up the logging (of swagger-core) or trying to create a small test case project to reproduce it so we can further investigate. I realize this may be difficult since you have your internal set up.
By the way, which version of CXF do you use?
The framework is using CXF 2.7.4.
How do I go about turning up the logging for swagger-core? Using a logback.xml file with a setting for io.swagger?
It will be a week or two before I can get to try and create a small test case, if I can at all. With this running on an in-house framework, it may be difficult to generate a test case that I can provide.
I realize this is not necessarily simple for production code, but can you try upgrading to CXF 2.7.15 and run it?
@cprokopiak - any update?
@webron Sorry, no updates yet. Haven't had a chance to come back to the issue. Hopefully later this week work will slow down a bit and give me an opportunity.
Closing due to inactivity