SpringBoot整合Swagger

导入swagger依赖pom.xml

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

SwaggerConfig自定义配置类

package com.xxz.base;

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()
                //不显示的接口路径映射
                .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("java", "http://atguigu.com", "1123@qq.com"))
                .build();
    }
}

控制器Controller代码[@Api/@ApiOperation/@ApiParam]

package com.xxz.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxz.edu.entity.Teacher;
import com.xxz.edu.entity.vo.TeacherVO;
import com.xxz.edu.service.TeacherService;
import com.xxz.util.Result;
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.context.annotation.Primary;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-30
 */
@Api("讲师模块")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {

    //注入service
    @Autowired
    private TeacherService teacherService;

    //查询讲师表所有数据
    //rest 风格
    @ApiOperation("查询所有讲师数据")
    @GetMapping("findAll")
    public Result findAll(){
        //调用service的方法实现查询所有的操作
        System.out.println(teacherService);
        List<Teacher> list = teacherService.list(null);
        return Result.ok().data("items", list);
    }

    //逻辑删除
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return Result.ok();
        }else{
            return Result.error();
        }

    }

    //分页查询
    @ApiOperation("讲师模块分页查询")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result pageList(@PathVariable long pageNum, @PathVariable long pageSize){

        //创建page对象
//        Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);
        Page<Teacher> teacherPage = new Page<>(1, 3);
        //调用方法实现分页,调用方法的时候底层封装,把分页的所有数据封装到pageTeacher对象中
        teacherService.page(teacherPage, null);
        //获取总记录数
        long total = teacherPage.getTotal();
        //获取集合
        List<Teacher> records = teacherPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    //条件查询带分页
    @ApiOperation("条件查询带分页")
    @PostMapping ("page/{pageNum}/{pageSize}")
    public Result pageConditionList(@PathVariable long pageNum,
                                    @PathVariable long pageSize,
                                    @RequestBody(required = false) TeacherVO teacherVO){
        //创建page对象
        Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);
        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper();
        //追加条件
        String name = teacherVO.getName();
        Integer level = teacherVO.getLevel();
        String begin = teacherVO.getBegin();
        String end = teacherVO.getEnd();
        //判断条件值是否为空,如果不为空拼接条件1
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level", level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create", begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }
        //调用方法实现条件查询分页
        teacherService.page(teacherPage, wrapper);
        //获取总记录数
        long total = teacherPage.getTotal();
        //获取集合
        List<Teacher> records = teacherPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    //新增讲师接口
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if(result){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    //修改前根据id查询讲师接口
    @PostMapping("getById/{id}")
    public Result getById(@PathVariable Integer id){
        //根据id查询讲师
        Teacher teacher = teacherService.getById(id);
        if(null != teacher){
            return Result.ok().data("teacher", teacher);
        }else{
            return Result.error();
        }
    }

    //修改讲师接口
    @PostMapping("update")
    public Result update(@RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

}


访问链接:[http://localhost/swagger-ui.html]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值