cancel
Showing results for 
Search instead for 
Did you mean: 

Swagger to generate docs from Java API code

Highlighted
New Contributor

Swagger to generate docs from Java API code

I need to automatically generate documentation from Java API code written by someone else using the Jersey framework. The project is built using Gradle. I'm trying to figure out how to use Swagger for this purpose but can't. I found a page (https://www.tutorialspoint.com/spring_boot/spring_boot_enabling_swagger2.htm) where it says I need to add some lines to the build.gradle file but I don't know where and also the page is for the Spring API.


How can I use Swagger here?

6 REPLIES 6
Highlighted
Staff

Re: Swagger to generate docs from Java API code

The tutorial you linked is using springfox and as you mention it allows to generate Swagger documentation analyzing Spring based APIs impementation code.

 

swagger-core (wiki) provides a similar functionality for JAX-RS based code (e.g your Jersey based API code), supporting recommended OpenAPI 3.0.0 as well as previous Swagger/OpenAPI 2.0 format (in the 1.5 branch , refer to this wiki page)

 

 

Highlighted
Community Manager

Re: Swagger to generate docs from Java API code

Thank you frantuma!

 

Hi @wally , does this information help?



Sonya Mihaljova
Community and Education Specialist

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!
Highlighted
New Contributor

Re: Swagger to generate docs from Java API code

I'm reading through those documents and trying to understand

Highlighted
New Contributor

Re: Swagger to generate docs from Java API code

After going through the articles I'm still not sure how to include Swagger in my project. I understand I need to add the dependency to the Swagger package but I don't know how exactly. I've tried adding these lines to the build.gradle file but can't see any effects:

plugins {
  id "io.swagger.core.v3.swagger-gradle-plugin" version "2.1.1"
}
Highlighted
Staff

Re: Swagger to generate docs from Java API code

This depends on what you want to achieve. The gradle config you shared ( I believe gotten from swagger-gradle-plugin README ) makes me think you want to build an OpenAPI specification out of JAX-RS based code as a file added to your project during build time. If this is the case you would define a gradle `resolve` task as described in README and execute it in your gradle build. 

 

If your goal is instead exposing an OpenAPI specification at runtime at an URI like https://<yourserver>/../openapi.json, you would follow the main wiki (the only difference being declaring the dependencies in your gradle file instead of the maven POM

Highlighted
New Contributor

Re: Swagger to generate docs from Java API code

Yes, I'm trying to do the first option (documentation generated at build time).

I'm afraid, I'll need some help with getting that 'resolve' task described (I'm not experienced with Gradle either). I see some code for the resolve task in the README page but I'm not sure where I'm supposed to add it. Somewhere in the build.gradle file, I presume, but where specifically?

resolve {
    outputFileName = 'PetStoreAPI'
    outputFormat = 'JSON'
    prettyPrint = 'TRUE'
    classpath = sourceSets.main.runtimeClasspath
    resourcePackages = ['io.test']
    outputDir = file('test')
}