DEV Community

Cover image for Swagger IU with Spring using Intellij Idea and Weblogic
Jorge Morales
Jorge Morales

Posted on • Updated on

Swagger IU with Spring using Intellij Idea and Weblogic

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

An expected output are like this
openapi_cli_expected_output

Now we open the project folder with IntelliJ Idea
Open_button_IntelliJ_Idea select_folder_project
Because we work with Weblogic server, we change the pom.xml
First change the package from jar to war
jar to war package
Add to the build tag the FinalName of the project
finalName_tag_example
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>
Enter fullscreen mode Exit fullscreen mode

pom_changed_plugins

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>
Enter fullscreen mode Exit fullscreen mode

added_dependencies

move the OpenAPI2SpringBoot to the com.test.bootloader for import them on the ServletInitializer

OpenAPI2SpringBoot and ServletInitializer at same folder

now create the package java and below com.test (in my case) them invoker into the package com.test.bootloader 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
    }
}
Enter fullscreen mode Exit fullscreen mode

servlet_initializer
Right click/Maven/Reload project to load the changes of the pom.xml
reload_maven_project
If fails, try using mvn install command
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

war_location
On the weblogic console at host:port/console, normally on localhost:7001/console enter your weblogic credentials and click on deployments
start_page_weblogic
Click install
install_button
Load war from files
load file button
Then select war file
Select_folder
then click next
next_button_then_select_file
next
next_uploaded_file
install deploy as app and next
deploy_as_app
verify the data and next
verify data
Select go to deployment page then finish
verify data
Onto deployment page go to control
select_control
Select the app onto the table the start/start serving all request
start_the_app
Below the tap test named you can see where the api is located
Image description
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

can be usefull this link for use log4j at this project

ko-fi

Top comments (0)