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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Extending Swagger and Springdoc Open API
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • Build Software Faster With Three Simple Principles
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Trending

  • OpenCode: Setting Up an Open-Source AI Coding Assistant
  • Why Role-Based Access Control Fails for AI Agents — and How to Fix It With Memory Lineage Engines
  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 29, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
23.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • Build Software Faster With Three Simple Principles
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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