Swagger IU with Spring using Intellij Idea and Weblogic
Tags: java, spring, swagger
Published: 2022-03-01 22:25:19

First, we need to download the open api generator from [openapi_gen](https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.1.0/openapi-generator-cli-5.1.0.jar)

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

```yml
#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](https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml)

```yml
#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

```bash
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
![openapi_cli_expected_output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvaoju4a2qc3yxt69nvw.png)

Now we open the project folder with IntelliJ Idea
![Open_button_IntelliJ_Idea](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygng4nrtqy7z9x7iwu8u.png) ![select_folder_project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0td6ch7ko5jwk6jpb5g1.png)
Because we work with Weblogic server, we change the pom.xml
First change the package from jar to war
![jar to war package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42yikfw7fr7qn29arkh4.png)
Add to the build tag the FinalName of the project
![finalName_tag_example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vu2a6iwfkbgr61n3vznx.png)
Add the gen folder to the source folder with this xml on the child of the plugins xml tags

```xml
<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>
```
![pom_changed_plugins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc5iox0992fxt63kv0mb.png)

Add this two dependencies bellow the dependecies tag

```xml
<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>
```
![added_dependencies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kq6x580hox50c2m7a7rf.png)

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

![OpenAPI2SpringBoot and ServletInitializer at same folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnrc5jdxl8d6rn0ezxf3.png)


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

```java
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
    }
}
```
![servlet_initializer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7bje7aoqrn41dtr4lfc.png)
Right click/Maven/Reload project to load the changes of the pom.xml
![reload_maven_project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64omaqs78177046jdpwb.png)
If fails, try using mvn install command
![mvn_install_command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hy8u9m9m23f02d9c6u9.png)
#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/daby8b02n4gfldpw1luw.png)
On the weblogic console at host:port/console, normally on localhost:7001/console enter your weblogic credentials and click on deployments
![start_page_weblogic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnxq90yytvyi87bec4cg.png)
Click install
![install_button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/la565uuf9gy4bz1jcd96.png)
Load war from files
![load file button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t45khp144duequatm62t.png)
Then select war file
![Select_folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt6uc98heyertyrewi7t.png)
then click next
![next_button_then_select_file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wor1xxatqo2yvdbc8fkp.png)
next
![next_uploaded_file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1le44o63s3nyx78q9qjm.png)
install deploy as app and next
![deploy_as_app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk3u2bl8l63ew1t6mu93.png)
verify the data and next
![verify data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dnpnzsy5tleyd101liq.png)
Select go to deployment page then finish
![verify data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtkico25p69nimub0nvx.png)
Onto deployment page go to control
![select_control](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi3koq6a2moxv2evyhlm.png)
Select the app onto the table the start/start serving all request
![start_the_app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qeh9zfh8cd1lf99t9tva.png)
Below the tap test named you can see where the api is located
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hqtrx4titg6oifvdkup.png)
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](https://dev.to/unbegrenzt/integrate-log4j2-to-java-spring-1h42) for use log4j at this project

[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/M4M1EJOMG)