how to enable swagger in quarkus examples

                         Enable swagger in quarkus examples

Purpose: In this post, we will learn how we can enable swagger in Quarkus and test API. The steps are given below.

1. Setup and Create Project: Visit hereCreate Quarkus Hello World Examples.

Next Step. Quarkus Parent Dependencies.

<properties>
        <compiler-plugin.version>3.8.1</compiler-plugin.version>
        <maven.compiler.parameters>true</maven.compiler.parameters>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>2.2.3.Final</quarkus.platform.version>
        <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Next Step. Project Dependencies.

       <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Next Step. Swagger Dependencies.

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-openapi</artifactId>
        </dependency>
Next Step. Create model.

package org.bce;

public class Employee {

    String id;
    String name;

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Employee(String idString name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Next Step. Create Employee resources.

package org.bce;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("/employees")
public class EmployeeResource {

    private Set<Employeeemployees = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    @GET
    public Set<Employeelist() {
        return employees;
    }

    @POST
    public Set<Employeeadd(Employee employee) {
        employees.add(employee);
        return employees;
    }

    @DELETE
    public Set<Employeedelete(Employee employee) {
        employees.removeIf(existingEmployee -> existingEmployee.name.contentEquals(employee.name));
        return employees;
    }
}
Next Step. Server Port:  The default server port is 8080. Wish you want to change open application.properties and add the below line.

quarkus.http.port=8081

Next Step. Open swagger URL in the browser.Click

Add Record : 
Fetch Records



Download Code from GitHub.  Source Code


No comments:

Post a Comment