SpringBoot整合Swagge3案例

x33g5p2x  于2022-02-21 转载在 Spring  
字(5.1k)|赞(0)|评价(0)|浏览(365)

1.pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>swagger3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>swagger3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--swagger依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.user类

package com.yl.swagger3.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "用户实体类",description = "一个定义用户信息的实体类")
public class User implements Serializable {
    @ApiModelProperty("用户id")
    private Integer id;
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

3.swagger配置

package com.yl.swagger3.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

// swagger的配置
@Configuration
public class SwaggerConfig {

    @Bean
    Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yl.swagger3.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(
                  new ApiInfoBuilder()
                        .description("xxx项目接口文档")
                        .contact(new Contact("root","http://www.baidu.com","xxx@qq.com"))
                        .version("V 1.0")
                        .title("Api测试文档")
                        .license("Apache2.0")
                        .licenseUrl("http://www.baidu.com")
                        .build()
                );
    }
}

4.controller

package com.yl.swagger3.controller;

import com.yl.swagger3.model.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
public class UserController {

    @GetMapping("/hello")
    public String hello() {
        return "hello swagger3";
    }

    @GetMapping("/getUserById/{id}")
    @ApiOperation(value = "查询用户",notes = "根据id查询用户") //接口详情
    @Operation(summary = "查询用户",description = "根据id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "id",required = true)
    })   //参数
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "500", description = "请求出错")
    }  // 响应码描述
    )
    public String getUserById(@PathVariable Integer id) {
        return "user1";
    }

    @ApiIgnore //忽略生成接口文档
    @GetMapping("hello1")
    public void hello1() {
        System.out.println("hello1");
    }

    @PostMapping("/addUser")
    @Parameter(name = "user",required = true) //参数
    public String addUser(@RequestBody User user) {
        return user.toString();
    }
}

5.swagger-ui访问地址,http://localhost:8080/swagger-ui/index.html

6.api接口地址,http://localhost:8080/v3/api-docs

相关文章