springboot集成swagger2自动生成接口文档

本文档介绍了如何在SpringBoot项目中集成Swagger2,通过添加相关依赖和配置,创建SwaggerConfig类,以及在Controller中添加注解,实现API接口的自动化文档生成。最后展示了如何访问Swagger2的UI页面查看和测试接口。
摘要由CSDN通过智能技术生成

增加依赖:

在pom.xml文件中增加加入Swagger2的依赖:记得在maven中install一下,如果还报红,则重启idea

<!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.13</version>
        </dependency>

创建Java类:

在包中创建一个package:config,里面创建一个类:SwaggerConfig.java,(这个包和类的位置没有要求)

package com.example.mybatisplus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.mybatisplus.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //RequestHandlerSelectors.basePackage(接口所在包路径)

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("springmvc swagger2")
                .contact("moyu")
                .version("1.1")
                .build();
    }
}

在controller类中增加注解:

@Controller
@RequestMapping("/api/admin")
@Api(value = "api接口", description="用户相关操作")
public class AdminController {

    private final Logger logger = LoggerFactory.getLogger( AdminController.class );

    @Autowired
    private AdminService adminService;

    /**
    * 描述:根据Id 查询
    *
    */
    @ApiOperation(value="查询用户", notes="根据url的id来查询指定对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Admin  admin =  adminService.getById(id);
        return JsonResponse.success(admin);
    }

    /**
    * 描述:根据Id删除
    *
    */
    @ApiOperation(value="删除用户", notes="根据url的id来删除指定对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        adminService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述:根据Id 更新
    *
    */
    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体admin", required = true, dataType = "Admin")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateAdmin(@PathVariable("id") Long  id,Admin  admin) throws Exception {
        admin.setId(id);
        adminService.updateById(admin);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Admin
    *
    */
    @ApiOperation(value="创建用户", notes="根据admin对象创建用户")
    @ApiImplicitParam(name = "admin", value = "用户详细实体user", required = true, dataType = "Admin")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Admin  admin) throws Exception {
        adminService.save(admin);
        return JsonResponse.success(null);
    }
}

swagger2常用属性:
请添加图片描述

运行项目,输入网址:

输入网址:http://localhost:8887/swagger-ui.html
请添加图片描述
参考链接:https://blog.didispace.com/springbootswagger2/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值