第一步、引入依赖

<!-- springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

第二步、在需要使用RestFul风格的@Controller标注的类上加入@EnableSwagger2注解

示例:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@RestController
@EnableSwagger2
public class UserController {
    
    @GetMapping("/hello")
    public String hello(){
        return "Hello Swagger-ui";
    }
}

第三步访问Swagger-ui文档界面

输入服务器ip+端口+/swagger-ui.html即可访问到swagger-ui的文档界面
例如: localhost:8080/swagger-ui.html
会访问到如下界面
在这里插入图片描述
在该界面可以直接测试RestFul风格的接口
在这里插入图片描述
点击Try it out即可尝试,如果有参数可以输入
在这里插入图片描述
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐