Swagger IU with Spring using Intellij Id ...

Swagger IU with Spring using Intellij Idea and Weblogic

Mar 02, 2022

First, we need to download the open api generator from openapi_gen

Is needed to create two files the Swagger definition and the Swagger Conguration

#Hello World Swagger definition file .yml
openapi: "3.0.2"
info:
  title: Hello World Swagger UI
  version: "1.0"
  description: Hello World Swagger UI
servers:
  - url: https://127.0.0.1/v1
components:
  schemas:
    result:
      title: result
      description: Hello world result!.
      type: object
      properties:
        message:
          type: boolean
          example: "Hello world request.name"
          description: Resolve the request for test this api.
    request:
      title: request
      description: request for /calldetails POST method
      type: object
      properties:
        name:
          type: string
          example: "Rivas"
          description: Name to test api
tags:
  - name: hello_world
    description: Hello world endpoints
paths:
  /hello:
    post:
      tags:
        - hello_world
      operationId: testHelloWorld
      summary: Resolve a hello_world for test.
      requestBody:
        description: return the request as a hello_world.
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/request"
      responses:
        "200":
          description: Found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/result"
        default:
          description: Unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/result"

On this file is the defined the swagger here a more explained example

#configHelloWorld.yml file
sourceFolder: "src/main/gen"
apiPackage: "com.test.api"
invokerPackage: "com.test.invoker"
basePackage: "com.test"
modelPackage: "com.test.model"
configPackage: "com.test.configuration"
serializableModel: true
artifactDescription: "API for hello_world project with sprint and Swagger UI"
artifactId: "TestingSwagger"
artifactVersion : "1.0"
groupId: "com.test"
developerOrganization   : "Test purposes"

The config file is for define the package where is genereated the source code

Now, execute the command using the downloaded jar file and the two config files

java -jar openapi-generator-cli-5.1.0.jar generate -g spring -i HelloWorldSwagger.yml -c configHelloWorld.yaml -o "HelloWorldProjectName"

An expected output are like this

Now we open the project folder with IntelliJ Idea

Because we work with Weblogic server, we change the pom.xml
First change the package from jar to war

Add to the build tag the FinalName of the project

Add the gen folder to the source folder with this xml on the child of the plugins xml tags

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/main/gen</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Add this two dependencies bellow the dependecies tag

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

now create the package java and below com.test (in my case) them invoker into the package com.test.invoker create the class ServletInitializer

package com.test.invoker;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import com.test.OpenAPI2SpringBoot;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(OpenAPI2SpringBoot.class); // Replace OpenAPI2SpringBoot with your main class
    }
}

Right click/Maven/Reload project to load the changes of the pom.xml

If fails, try using mvn install command

Now for deploy to weblogic

when executes mvn install or mvn deploy he builds the .war file onto target/app.war these is builded .war for deploy manually to weblogic

On the weblogic console at host:port/console, normally on localhost:7001/console enter your weblogic credentials and click on deployments

Click install

Load war from files

Then select war file

then click next

next

install deploy as app and next

verify the data and next

Select go to deployment page then finish

Onto deployment page go to control

Select the app onto the table the start/start serving all request

Below the tap test named you can see where the api is located

Remember the correct url path is protocol://host:port/app_name/swagger-ui.html in this case the correct path is: http://172.26.214.140:7001/TestingSwagger/swagger-ui.html

Enjoy this post?

Buy unbegrenzt a coffee

More from unbegrenzt