DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Extending Swagger and Springdoc Open API
  • How to Detect Spam Content in Documents Using C#
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • How to Merge HTML Documents in Java

Trending

  • Reactive Kafka With Spring Boot
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)
  1. DZone
  2. Data Engineering
  3. Databases
  4. Static API Documentation With Spring and Swagger

Static API Documentation With Spring and Swagger

Explore static API documentation with Spring and Swagger.

By 
Eitan Nosraty user avatar
Eitan Nosraty
·
May. 29, 19 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
23.2K Views

Join the DZone community and get the full member experience.

Join For Free

Swagger is an open-source framework to document REST APIs, and it has become the de-facto standard. With spring, you can easily expose a test page for all your REST APIs using swagger-ui. The Swagger framework also allows the developers to generate API documents automatically and expose them via a web server.

The problem starts when we need to send these API documents without any server. For this purpose, I built a service to produce and expose these API documentations. This documentation will be daily updated by Jenkins automation server.

In order to produce the document, I used 2 plugins:

swagger2markup-maven-plugin: In order to convert from Swagger.json to asciidoc format.

asciidoctor-maven-plugin: In order to convert from asciidoc file to an HTML file that will be served later by my service.

The configuration of those plugins resides in the project’s Pom.xml:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
    <groupId>io.github.swagger2markup</groupId>
    <artifactId>swagger2markup-maven-plugin</artifactId>
    <version>1.3.3</version>
    <configuration>
        <swaggerInput>${project.build.directory}/generated-sources/swagger.json</swaggerInput>
        <outputDir>${project.build.directory}/generated-sources/swagger/</outputDir>
        <config>
            <swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
        </config>
    </configuration>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>convertSwagger2markup</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-maven-plugin</artifactId>
    <version>1.5.3</version>
    <dependencies>
        <dependency>
            <groupId>org.jruby</groupId>
            <artifactId>jruby-complete</artifactId>
            <version>1.7.21</version>
        </dependency>
    </dependencies>
    <configuration>
        <sourceDirectory>${project.basedir}/src/main/asciidoc/</sourceDirectory>
        <sourceDocumentName>index.adoc</sourceDocumentName>
        <backend>html5</backend>
        <outputDirectory>${project.build.directory}/generated-sources/swagger-html/</outputDirectory>
        <attributes>
            <toc>left</toc>
            <toclevels>3</toclevels>
            <generated>${project.build.directory}/generated-sources/swagger/</generated>
        </attributes>
    </configuration>

    <executions>
        <execution>
            <id>output-html</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
        </execution>
    </executions>
</plugin>
</plugins>
</build>

After that, create an index.adoc file in src/main/asciidoc with the content:

include::{generated}/overview.adoc[]

include::{generated}/paths.adoc[]

include::{generated}/definitions.adoc[]

The static document is created from the service we want to document, and for that, we need the swagger running on that service. Swagger configuration should be added to the micro-service as follows:

Pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>

SwaggerConfig.java

@EnableSwagger2
@Configuration
public class SwaggerConfig {

@Bean
public Docket docket(ApiInfo apiInfo) {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("user-api")
        .useDefaultResponseMessages(false)
        .apiInfo(apiInfo)
        .select().apis(RequestHandlerSelectors.basePackage("com.swagger2doc")).paths(PathSelectors.any())
        .build();
}
}

The Swagger.json will be created with SpringBootTest, sending rest call to the swagger -> http://ip:port/v2/api-docs?-api and create the JSON file.

@SpringBootTest
public class Swagger2docApplicationTests {

    private TestRestTemplate restTemplate = new TestRestTemplate();


    @Test
    public void testHome() throws Exception {

    ResponseEntity<String> res = restTemplate.getForEntity("http://ip:port/v2/api-docs?group=user-api", String.class);

        String swagger = res.getBody();

        this.writeFile("swagger.json", swagger);
    }

    public void writeFile(String fileName, String content) {

    File theDir = new File("target/generated-sources");

    if (!theDir.exists()) {
    try{
    theDir.mkdir();
    } 
    catch(SecurityException se){ }        
    }

    BufferedWriter bw = null;
    FileWriter fw = null;
    try {
    fw = new FileWriter(theDir + "/" + fileName);
    bw = new BufferedWriter(fw);
    bw.write(content);
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (bw != null)
    bw.close();
    if (fw != null)
    fw.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    }

    }
}

After the Maven build, the static document will be generated in target/generated-sources/swagger-html.

In case you want to generate documentation for several services at once, you can get the list of URLs from application.properties and then run Maven from inside the Junit (via System.exec or maven java client) for each file, not forgetting to rename the output file after each generation to avoid it being overrun by the next iteration’s output file.

In order to generate the daily's document, the build will be run by Jenkins's job and will produce the updated one.

API Documentation Spring Framework Document

Opinions expressed by DZone contributors are their own.

Related

  • Extending Swagger and Springdoc Open API
  • How to Detect Spam Content in Documents Using C#
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • How to Merge HTML Documents in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook