Swagger -----自动生成接口文档

测试demo:Spring+SpringMvc+Mybatis-Plus+SpringBoot+RESTful风格Api +Maven

1,引入依赖

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

2,SwaggerConfig

package com.mangoubiubiu.config;

import com.google.common.base.Predicates;
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.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;

@Configuration//配置类
@EnableSwagger2//swagger注解
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //接口中包含admin error 就不显示
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }


    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("Mangoubiubiu", "https://www.cnblogs.com/mangoubiubiu/", "0000000000@qq.com"))
                .build();
    }

}

3,Controller

package com.mangoubiubiu.edu.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mangoubiubiu.commonutils.R;
import com.mangoubiubiu.edu.entity.Teacher;
import com.mangoubiubiu.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author mangoubiubiu
 * @since 2020-09-22
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class TeacherController {

    @Autowired
    private TeacherService service;

    //rest风格

    /**
     * 查询所有的教师信息
     * @return
     */
    @ApiOperation(value = "所有的教师信息")
    @GetMapping("findAll")
    public R findAllTeacher(){


        List<Teacher>  list= service.list(null);
        return R.ok().data("items",list);
    }

    /**
     * @DeleteMapping("{id}") 通过路径传递
     * @PathVariable String id 得到路径中的id值
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除接口")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name="id",value = "讲师ID",required = true) @PathVariable String id){
        boolean flag=service.removeById(id);
        if(flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }


    /**
     *分页
     * @param current 当前页
     * @param limit 记录数
     * @return
     */
    @ApiOperation(value = "查询教师信息分页接口")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name="current",value = "当前页",required = true) @PathVariable Long current,
                             @ApiParam(name="limit",value = "记录数",required = true) @PathVariable Long limit){
        //current:当前页,size:每页的记录数
        Page<Teacher> pageInfo=new Page<>(current,limit);

        //调用方法时底层会做封装,封装到pageInfo里面
        service.page(pageInfo,null);
        long total= pageInfo.getTotal();//总记录数
        List<Teacher> records = pageInfo.getRecords();//所有的数据list

        return R.ok().data("total",total).data("rows",records);

    }


    /**
     * 添加教师接口
     * @param
     * @return
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher eduTeacher){
        boolean flag=  service.save(eduTeacher);
        if(flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 根据讲师id查询讲师信息
    @GetMapping("getTeacher/{id}")
    public  R getTeacher(@PathVariable String id){
        Teacher eduTeacher = service.getById(id);
        return R.ok().data("teacher",eduTeacher);

    }
    //讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher eduTeacher){
        boolean flag= service.updateById(eduTeacher);
        if(flag==true){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

 

访问地址:http://127.0.0.1:8001/swagger-ui.html

效果:

 

 

 

 

 

 

posted @ 2020-09-24 23:17  KwFruit  阅读(269)  评论(0编辑  收藏  举报