Swagger (3): Swagger entry case

1 Write SpringBoot project

Create a new Rest request controller.

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @PostMapping("/post")
    public String post(){
        return "post";
    }

    @GetMapping("/get")
    public String get(String a,String b){
        return "get";
    }

    @RequestMapping("/req")
    public String req(String m){
        return "req";
    }
}

2 Import Spring-fox dependencies

Import the Spring-fox dependency in the project's pom.xml. The latest version is currently 2.9.2, so the imported dependencies are also of this version. Among them, springfox-swagger2 is the encapsulation of the core content. springfox-swagger-ui is an encapsulation of swagger-ui.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>swaggerDemo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>
</project>

3 Add annotations

Add the @EnableSwagger2 annotation to the SpringBoot startup class.

Adding this annotation means scanning all controllers in the current project. Apply Swagger2

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class);
    }
}

4 Access swagger-ui

After starting the project, enter http://ip:8080/swagger-ui.html in the browser to access the swagger-ui page, where you can visually operate all interfaces in the project.

5 swagger-ui

After accessing swagger-ui.html, you can see the names of all controllers that need to generate interface documents on the page.

Each controller contains various access methods for all controller methods. If @RequestMapping is used for mapping, all request methods below will be displayed. If you use @PostMapping, only the Post method can be accessed, and only the Post one is shown below.

Click on a request method to try it out

An interface will appear asking for input values. After completing the input, click the Execute button

The Request URL and the results returned by different status codes will appear below.

Guess you like

Origin blog.csdn.net/u013938578/article/details/134481241