栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Swagger2笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swagger2笔记

添加pom依赖

    io.springfox
    springfox-swagger2
    2.8.0


    io.springfox
    springfox-swagger-ui
    2.8.0

配置类
@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 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号