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

Now we open the project folder with IntelliJ Idea
图片[1]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋 图片[1]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Because we work with Weblogic server, we change the pom.xml
First change the package from jar to war
图片[2]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Add to the build tag the FinalName of the project
图片[3]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
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

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

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

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

图片[4]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Right click/Maven/Reload project to load the changes of the pom.xml
图片[5]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
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

图片[6]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
On the weblogic console at host:port/console, normally on localhost:7001/console enter your weblogic credentials and click on deployments
图片[7]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Click install
图片[8]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Load war from files
图片[9]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Then select war file
图片[10]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
then click next
图片[11]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
next
图片[12]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
install deploy as app and next
图片[13]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
verify the data and next
图片[14]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Select go to deployment page then finish
图片[15]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Onto deployment page go to control
图片[16]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Select the app onto the table the start/start serving all request
图片[17]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
Below the tap test named you can see where the api is located
图片[18]-Swagger IU with Spring using Intellij Idea and Weblogic - 拾光赋-拾光赋
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

原文链接:Swagger IU with Spring using Intellij Idea and Weblogic

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容