天天看点

swagger的引入以及使用

作者:让我胖起来

1、pom.xml 引入依赖

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

2、新建swagger配置类

package com.zxx.until;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

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

@EnableWebMvc
@EnableSwagger2 //Loads the spring beans required by the framework
public class MySwaggerConfig {

    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()  // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.any()) // 对所有api进行监控
                // .apis(RequestHandlerSelectors.basePackage("")) 指定监控路径
                .paths(PathSelectors.any()) // 对所有路径进行监控
                .build();
    }
}           

3、在SpringMVC的Bean中写入资源路径映射

<!-- 引入swagger相关 -->
<bean class="com.zxx.controller.MySwaggerConfig"/>   <!-- 配置类全路径 -->           

4、改造controller

类上加: 
@Api(tags = “student”) 自定义名字,用来描述接口
方法上加: 描述功能和参数 ,例: 
@ApiOperation(value=”获取用户列表”,notes=”获取用户列表”) 
@PostMapping(value = “/list002”) 
@ApiImplicitParams({ 
@ApiImplicitParam(name = “name”, value = “姓名”, paramType = “query”, dataType = “String”,required = true), 
@ApiImplicitParam(name = “age”, value = “年龄”, paramType = “query”, dataType = “String”,required = true), 
@ApiImplicitParam(name = “id”, value = “ID”, paramType = “query”, dataType = “String”), 
}) 
public String queryList(Student user){ 
   System.out.println(“姓名:”+user.getId()); 
   System.out.println(“年龄:”+user.getName());  
   return “aa”; 
}           
简单介绍常用注解 
@ApiIgnore 忽略不显示该接口(类和方法上都可以加) 
@PostMapping(value = “/list002”) 发送get请求 
@GetMapping(value = “/list002”) 发送post请求 
@ApiImplicitParams 参数描述 多个参数需嵌套 @ApiImplicitParam 
@ApiImplicitParam 参数描述 只有一个参数可以直接用 
  name–参数名 
  value–参数说明 
  required = true / false 是否必须输入 
  dataType = “String” 参数类型 
  defaultValue = “” 默认值 
  allowableValues = “xx,xx,xx”  下拉内容 
  allowMultiple = true 下拉支持多选 
  paramType = “query” 
    path 以地址的形式提交数据 
    query 直接跟参数完成自动映射赋值 
    body 以流的形式提交 仅支持POST 
    header 参数在request headers 里边提交 
    form 以form表单的形式提交 仅支持POST

如果直接传递对象,可在参数直接加 RequestBody 注解 例:@RequestBody Student student

如果将参数拼到url上 ,例: 
@ApiOperation(value = “”, notes = “”) 
@PostMapping(value = “queryById/{id}”) 
public String getQrcode(@PathVariable String id) { 
  return “”; 
}           

5.直接访问 ip+port 自定义

http://localhost:8080/项目名/swagger-ui.html