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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Extending Swagger and Springdoc Open API
  • Spring Microservices RESTFul API Documentation With Swagger Part 1
  • How to Merge HTML Documents in Java
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  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
22.7K 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
  • Spring Microservices RESTFul API Documentation With Swagger Part 1
  • How to Merge HTML Documents in Java
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!