其他分享
首页 > 其他分享> > SpringBoot + Swagger2

SpringBoot + Swagger2

作者:互联网

POM.xml

高版本的springboot需要做些调整。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--swagger-->
        <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>

启用swagger

启动器上新增@EnableSwagger2注解。

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

写一个测试Controller

package com.autumn.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(value = "用户api", tags = "权限api")
public class UserController {

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加信息")
    private String add(){
        return true?"数据新增":"数据新增失败";
    }


    @GetMapping("/delete")
    @ResponseBody
    private String delete(){
        return false?"数据删除成功":"数据删除失败";
    }

    @GetMapping("/update")
    @ResponseBody
    private String update(){
        return true?"数据更新成功":"数据更新失败";
    }

    @GetMapping("/query")
    @ResponseBody
    private String query(){
        return "获取列表!";
    }

}

访问swagger

http://localhost:8080/swagger-ui.html
image

标签:SpringBoot,spring,boot,springframework,Swagger2,org,swagger,String
来源: https://www.cnblogs.com/aeolian/p/16434752.html