How use swagger with jersey2 ResourceConfig

3,205 views
Skip to first unread message

Karsten

unread,
Feb 20, 2014, 6:04:19 AM2/20/14
to swagger-sw...@googlegroups.com
Hi,

I got the following ResourceConfig with no web.xml

@ApplicationPath("/resources")
public class RESTConfig extends ResourceConfig {

public RESTConfig() {
packages("my.packages", "com.wordnik.swagger.jersey.listing");
register(RolesAllowedDynamicFeature.class);
}
}

Now the problem is I have no idea how to set api basepath :-(

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

Another thing that puzzles me is the swaggerVersion, why is it 1.2 when I am using 1.3.2

<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<version>1.3.2</version>
</dependency>

Can someone help me with getting swagger up and running?

Ron

unread,
Feb 20, 2014, 6:33:13 AM2/20/14
to swagger-sw...@googlegroups.com
Karsten,

To get Swagger to run without a web.xml, please look at the following blog post - http://jmchung.github.io/blog/2013/12/14/integrating-swagger-into-jax-rs-with-java-ee-6-specification/. If that doesn't help, write back and we'll assist you from there.

As for swaggerVersion being 1.2 - that refers to the version of the Swagger Specification. 1.3.2 is the version of the library you use, and does not reflect directly on the version of spec it supports (which is 1.2).


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Karsten

unread,
Feb 20, 2014, 6:54:51 AM2/20/14
to swagger-sw...@googlegroups.com
Ron,
thanks for your fast answer. The link doesn't help me, still all my tries to configure swagger fail. The Specify a Swagger Configuration Class part is my problem I think. Using SwaggerConfig.class doesn't work and JerseyJaxrConfig.class doesn't offer any methods for configuration. The Specify the swagger package part is done by my packages("com.wordnik.swagger.jersey.listing") and since I see the output I hope/think that works.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron

unread,
Feb 20, 2014, 8:29:58 AM2/20/14
to swagger-sw...@googlegroups.com
Did you add the ApplicationConfig.java class from that link?


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Ron

unread,
Feb 20, 2014, 8:30:55 AM2/20/14
to swagger-sw...@googlegroups.com
Sorry, I meant SwaggerJaxrsConfig.java

Also, what do you mean doesn't work? Can you share your code?

Karsten

unread,
Feb 20, 2014, 8:52:53 AM2/20/14
to swagger-sw...@googlegroups.com
Yeah I added the class and changed the basePath like this:
 swaggerConfig.setBasePath("http://localhost:8080/myApp/resources");
But there was no effect one the output @ http://localhost:8080/myApp/resources/api-docs it still is {"apiVersion":"0.0","swaggerVersion":"1.2"}.
MyApp is running on a tomcat7.

What code do you need?
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Ron

unread,
Feb 20, 2014, 8:57:23 AM2/20/14
to swagger-sw...@googlegroups.com
Try this instead of using the changes from the blog:

package com.company.swagger.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.wordnik.swagger.jaxrs.config.BeanConfig;

@WebListener
public class SwaggerInitializer implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion( "1.0.2" );
        beanConfig.setResourcePackage( "com.company.swagger.domain.rest" ); // replace with your packages
        beanConfig.setBasePath( "http://localhost:8080/myApp/resources/" );
        beanConfig.setDescription( "My RESTful resources" );
        beanConfig.setTitle( "My RESTful API" );
        beanConfig.setScan( true );
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
   
}


But make sure you replace the package at the line with the comment to your own package (the one containing your annotated resources).


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Karsten

unread,
Feb 20, 2014, 9:30:57 AM2/20/14
to swagger-sw...@googlegroups.com
Ron,

that's great, it works :-) 
The output is now:
"apiVersion":"1.0.2","swaggerVersion":"1.2","apis":[{"path":"/users","description":"Operations about pets"}],"info":{"title":"My RESTful API","description":"My RESTful resources"}}
and I can access "http://localhost:8080/myApp/resources/api-docs/users" and there I see the available operations.

Two questions left :-)
How can I set a reponse type like List<User> for an index action?
Is pretty print an option for swagger? ;-)

Thanks again!

Ron

unread,
Feb 20, 2014, 9:38:38 AM2/20/14
to swagger-sw...@googlegroups.com
As for pretty print support, I don't think swagger-core directly supports pretty-printing, but since it uses Jackson, you can probably hack around and get it to pretty-print instead (I don't have an off-hand solution).


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Karsten

unread,
Feb 20, 2014, 9:48:31 AM2/20/14
to swagger-sw...@googlegroups.com
Thanks again for the awesome help :-)
Reply all
Reply to author
Forward
0 new messages