Hi,
On 01/03/13 04:46, Raphael Vullriede wrote:
Hi Sergey,


On 02/28/2013 11:34 PM, Sergey Beryozkin wrote:
Hi
On 28/02/13 04:47, Raphael Vullriede wrote:
Hi,

for automated API documentation I tried to get Swagger
(http://developers.helloreverb.com/swagger/) to work with CXF. It is
really a beautiful way to document your rest services and it would be
great if we can get this work with CXF. Currently it doesn't work
because of the following problem:

Their implementation for API resource listing relies on the injection of
two objects via @Context annotation:

javax.servlet.ServletConfig
javax.ws.rs.core.Application

The first one works, the second one doesn't and returns null instead.

When I look at the documentation at
http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Contextannotations,

it seems Application is not supported for injection.


What resource class is Application expected to be injected in ? I've
looked at a sample app and haven't seen Application reference. I'm
pretty sure the injection of Application classes is supported - we
have tests for that - specifically when Application is provided as
part of CXFNonSpringJaxrsServlet,

It is expected to be injected into the ApiListingResource, which need
this to auto-detect all other ressource listings. It is defined in
applicationContext.xml:

<!-- Swagger API listing resource -->
<bean id="swaggerRessource"
class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />

This is class is not visible in your GIT repo

and registered like this (after follwing your suggesting and using
JAXRSServerFactoryBean directly):

<bean class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean"
init-method="create">
<property name="address" value="/v1.0" />
<property name="serviceBeans">
<list>
<ref bean="userRessource" />
<ref bean="swaggerRessource" />
</list>
</property>
<property name="providers">
<list>
<ref bean="jsonProvider" />
</list>
</property>
</bean>


Note JAXRSServerFactoryBean has a setApplication method - so if the
factory has this property set then the injection of Application, if
required, will be done.

< snip />

and inject Application if any,

I've seen the setApplication() method but I don't really know what I
should define here. I didn't find any implementation of Application in
CXF that I could instantiate manually.


The JAX-RS endpoint can be started from Spring and in such cases no explicit Application is available, Application is what developers are supposed to provide if no Spring or other injection support is available.

For example, http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java

and at the moment a custom Application can be registered with CXFNonSpringJaxrsServlet,

So when you use Spring, no Application is available, as Spring context gives the runtime the info Application would give it otherwise if no Spring were used.

Perhaps, CXF could've syntesized Application if "@Context Application" were available, but that probably would go a bit too far, as in most cases one would inject Application itself to get to some application properties like "defaultName" (in the linked resource), etc.

I think the workaround, when also working with Spring, is to create a basic Application and only list resource class objects as it probably all Swagger needs and set it on the factory, in addition to the above injection of resources/providers - that won't affect the endpoint initialization, not at the moment.

Cheers, Sergey




Thanks,
Raphael


The ticket for Swagger is here:
https://github.com/wordnik/swagger-core/issues/82
A sample app can be found here:
https://github.com/rvullriede/swagger-core/tree/master/samples/swagger-java-sample-app-cxf



To get the sample to work you have to add the sonatype snapshot
repository (https://oss.sonatype.org/content/repositories/snapshots/) to
you maven configuration.

After that you can run the sample with mvn tomcat:run and access the app
under context /swagger-cxf

GET http://localhost:8080/swagger-cxf/api => CXF Servlet Endpoint
GET http://localhost:8080/swagger-cxf/api/v1.0?_wadl => autogenerated
WADL for CXF service /v1.0
GET http://localhost:8080/swagger-cxf/api/v1.0/user.json/user1 =>
example request for user resource

GET http://localhost:8080/swagger-cxf/api/v1.0/api-docs.json => returns
an NPE because of missing Application.

Does anybody have an idea how to get this work?

Thanks for your help,
Raphael






Reply via email to