swagger2的使用

添加swagger依赖:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

  

swagger2配置类:

@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.enable}") //开启访问接口文档的权限  **swagger.enable是在yml配置文件中配置为true**
public class Swagger2 {

    @Bean
    public Docket userRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户模块")  //模块名称
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.mybatisplustest.controller"))  //扫描的控制器路径
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx项目开发接口文档")    //接口文档标题
                .description("此文档仅供开发技术组领导、开发人员使用")   //描述
                .termsOfServiceUrl("http://www.baidu.com/")   //相关的网址
                .contact(new Contact("后端开发","http://www.xxx.com/","XXXXXX@qq.com"))    //作者  邮箱等
                .version("1.0")  //版本号
                .build();
    }
}

  

在Controller中的用法:

@RestController
@RequestMapping("/user")
//@Api(tags = "测试,产品,作用类上")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getList")
    //@ApiOperation(value = "获取所有的产品信息",httpMethod ="GET",notes = "展开后的信息提示,可以写的比较详细")
    public String getList() {
        List<User> list = userService.list();
        String s = "";
        for (User user : list) {
            s += user.toString();
            s += "<br>";
        }
        return s;
    }
}

 

查看接口文档:http://localhost:8080/swagger-ui/index.html

posted @ 2022-04-20 10:00  陈超阿  阅读(197)  评论(0)    收藏  举报