Swagger2笔记

添加pom依赖
<!--Swagger2依赖-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>
配置类
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket createRestAPI(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dou"))//扫描的包
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("豆豆接口服务")//设置文档的标题
                .description("豆豆几口服务,API接口文档")//设置文档的描述
                .version("1.0")//设置文档的版本
                .termsOfServiceUrl("http://www.doudou.com")//设置文档License信息
                .build();
    }
}

@Configuration:让Spring加载该配置类

@EnableSwagger2:启动Swagger2

代码实现
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation(value = "获取用户列表",notes = "")
    @RequestMapping(value = "/getStudents", method = RequestMethod.GET)
    public List<Student> getStudents(){

        return studentService.getStudents();
    }

    @ApiOperation(value = "增加学生信息", notes = "根据student添加学生信息")
    @ApiImplicitParam(name = "student", value = "添加学生信息", required = true, dataType = "Student", dataTypeClass = Student.class)
    @RequestMapping(value = "/postStudent", method = RequestMethod.POST)
    public String postStudent(@RequestBody Student student){

        studentService.postStudent(student);

        return "success";
    }

    @ApiOperation(value = "更新学生信息", notes = "根据id来取出更新学生信息,把用户更信息后修改数据库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "student", value = "学生信息", required = true, dataType = "Student", dataTypeClass = Student.class)
    })
    @RequestMapping(value = "/putStudent", method = RequestMethod.PUT)
    public String putStudent(@RequestParam String id, @RequestBody Student student){

        Student stu = studentService.getStudent(id);
        stu.setName(student.getName());
        stu.setAge(student.getAge());
        studentService.putStudent(stu);

        return "success";
    }

    @ApiOperation(value = "删除学生", notes = "根据id删除学生信息")
    @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "String", dataTypeClass = String.class)
    @RequestMapping(value = "/deleteStudent", method = RequestMethod.DELETE)
    public String deleteStudent(@RequestParam String id){
        studentService.deleteStudent(id);

        return "success";
    }
}

@ApiOperation:描述接口

  • value:该接口的简单描述
  • notes:操作的详细描述

@ApiImplicitParam:描述一个参数

  • name:参数名称
  • value:参数的简单描述
  • required:是否需要该参数
  • dataType:参数的数据类型
  • dataTypeClass:参数的类

@ApiImplicitParams:描述多个参数

@Apilgnore:使用该注解忽略这个API

成功

代码完成后启动项目,访问:http://ip:端口号/swagger-ui.html

在这里插入图片描述

问题
  1. 版本问题
    • Unable to interpret the implicit parameter configuration with dataType: String, dataTypeClass: class java.lang.Void

      一般对于integer、long、string这样的类型,会遇到此报错,

      **解决方法:**降低swagger版本,我原先版本3.0.0,降低为2.8.0,不在出现此报错

    • Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:

      **解决方法:**spring boot项目整合swagger需要注意两者的版本,spring boot项目的版本低,相应的swagger版本不能太高,反之亦然,将swagger版本更改到与spring boot版本相近即可。

  2. 配置问题
    • Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

      原因:Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.x使用的是PathPatternMatcher

      **解决方法:**使用yaml,添加如下配置

      spring:
        mvc:
          pathmatch:
            matching-strategy: ANT_PATH_MATCHER
      

      使用properties配置,添加如下配置

      spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER
      
代码地址

https://gitee.com/shen_doudou/practice-code.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值