Re: Can't get Swagger working with Jax-rs + Tomcat

1,101 views
Skip to first unread message
Message has been deleted

Ron Ratovsky

unread,
Oct 1, 2015, 7:47:59 PM10/1/15
to Swagger
Hi there!

Welcome to Swagger.

The first step would be to upgrade to the latest version which is 1.5.3 (notice, not 1.5.3-M1). This also carries a name space change to the artifact and the package names.
Simply replace com.wordnik.swagger with io.swagger for the packages and com.wordnik to io.swagger for the artifact groupId.

If that doesn't resolve the issue, please share your Application class (com.abc.def.content.service.ContentService).

On Thu, Oct 1, 2015 at 12:59 PM, <redfla...@hotmail.com> wrote:
Hi,

I've created a very simple REST service using Java and Tomcat, and it is working fine. But when I load up Swagger-UI, and pass in "http://localhost:8080/Content-Server/service/content/hello", I get an error: Uncaught TypeError: Cannot read property 'definitions' of null. CORS isn't the issue I believe as I've fixed that.

Here are some of my files (the important parts of them):
@Path("/content")
@Api(value="/content", description="Manage content")
public class Content {
   
   
private final static Logger LOGGER = Logger.getLogger(Content.class.getName());
   
   
@GET
   
@Path("/hello")
   
@Produces(MediaType.APPLICATION_JSON)
   
@ApiOperation(value="default", httpMethod="GET")
   
public JSONObject hello() {
       
JSONObject json = new JSONObject();
       
try {
            json
.put("hello", "world");
       
} catch (JSONException e) {
           
// TODO Auto-generated catch block
            e
.printStackTrace();
       
}
       
return json;
   
}
}
Content.java

<web-app>
   
<display-name>Content Stuff</display-name>
   
<servlet>
       
<servlet-name>rest</servlet-name>
       
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
       
<init-param>
           
<param-name>javax.ws.rs.Application</param-name>
           
<param-value>com.abc.def.content.service.ContentService</param-value>
       
</init-param>
       
<load-on-startup>1</load-on-startup>
   
</servlet>
       
   
<servlet>
       
<servlet-name>DefaultJaxrsConfig</servlet-name>
       
<servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
       
<init-param>
           
<param-name>api.version</param-name>
           
<param-value>1.0.0</param-value>
       
</init-param>
       
<init-param>
             
<param-name>swagger.api.basepath</param-name>
           
<param-value>/service</param-value>
       
</init-param>
       
<load-on-startup>2</load-on-startup>
   
</servlet>
   
   
<servlet-mapping>
       
<servlet-name>rest</servlet-name>
       
<url-pattern>/service/*</url-pattern>
   
</servlet-mapping>
</web-app>
web.xml

<dependencies>
     
<dependency>
         
<groupId>com.wordnik</groupId>
         
<artifactId>swagger-jaxrs</artifactId>
         
<version>1.5.3-M1</version>
     
</dependency>
</dependencies>
pom.xml

I'm very new at Swagger (and Java REST services) but I'd appreciate any help. Thanks!

--
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/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

redfla...@hotmail.com

unread,
Oct 2, 2015, 9:01:53 AM10/2/15
to Swagger
Hi Ron,

Thanks for the welcome and the reply! I've made the changes that you listed but the issue persists. Here is the ContentService.java file:
public class ContentService extends Application {
   
   
@Override
   
public Set<Class<?>> getClasses() {
       
Set<Class<?>> classes = new HashSet<Class<?>>();
        classes
.add(Content.class);
       
return classes;
   
}
 
}


Message has been deleted

Mike Redd

unread,
Oct 2, 2015, 11:04:20 AM10/2/15
to Swagger
I did not follow the "Package scanning / Concrete class selection" section of the setup guide, as other guides on the internet did not either. May that be the reason?

Mike Redd

unread,
Oct 2, 2015, 12:13:41 PM10/2/15
to Swagger

Ron Ratovsky

unread,
Oct 2, 2015, 1:36:43 PM10/2/15
to Swagger
No worries, Mike.

The swagger.json isn't saved anywhere, it is generated at runtime, so you won't find it.
The swagger.json would be hosted directly under the context root of your application. In fact, it is just a jax-rs resource like any other you have, with the @Path("/swagger.json") value (hope this makes sense).

If you're getting a CNFE on the DefaultJaxrsConfig, then it means something is still a bit off with the pom.xml or there could be a possible caching issue.
Can you share your complete pom.xml? If you can't, please provide the output of 'mvn depdency:list | grep swagger'.

--
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/d/optout.

Mike Redd

unread,
Oct 2, 2015, 2:07:58 PM10/2/15
to Swagger
Hi Ron,

I got it working! I'll post the issues that needed fixing in case someone encounters something similar in the future:

1. Missing libraries. There were many and I was able to detect which ones by repeatedly trying to explore the swagger.json file from Swagger UI interface.
2. Had to add the necessary two lines to the Application subclass getClasses() method, as suggested in https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#using-the-application-class under the Using a custom Application subclass section.

Thanks a lot for the help Ron! Swagger was a very exciting discovery for me, and I look forward to learning more about it as I continue building my application.

Ron Ratovsky

unread,
Oct 2, 2015, 2:48:41 PM10/2/15
to Swagger
Happy to hear it all sorted out!

Feel free to ask anything else should you encounter issues in the future.

--
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/d/optout.

Sathish Kumar

unread,
Oct 21, 2017, 8:01:50 PM10/21/17
to Swagger
Hello Mike,
can you please share the changes that you have done.
I am currently not able to turn the swagger UI on, I do have swagger.json produced on the context root level. But having hard time to hook up the swagger ui.
please share the solution. It would be great to have my swagger up and running :)

kaushal Shah

unread,
May 4, 2018, 6:16:06 AM5/4/18
to Swagger
Hi Ron,

I tried to integrate swagger But its not generating swagger.json
I added maven dependency, servlet in web.xml and @API annotation in one service.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages