swagger

导入依赖

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

设置swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 配置Swagger的Docket的bean实例
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("组名")
                .select()
                // 配置扫描接口的方式
                .apis(RequestHandlerSelectors.basePackage("com.lin"))
                .build();
    }

    /**
     * 配置Info信息
     * @return
     */
    private ApiInfo apiInfo(){
        // 作者信息
        Contact contact = new Contact("", "", "");
        return new ApiInfo("我是标题",
                "描述",
                "1.0", "urn:tos",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());

    }
}
  • 设置扫描接口方式RequestHandlerSelectors.
    • basePackage(“com.lin”)配置要扫描的接口
    • any():扫描包
    • none():不扫描

常用注解

  • 实体类
    • @ApiModel("实体类名")
    • @ApiModelProperty("属性名")
@Data
@ApiModel("学生")
public class Student {
    @ApiModelProperty("学号")
    private Long studentId;
    @ApiModelProperty("学生姓名")
    private String studentName;

    private Integer studentAge;

    private List<Course> courseList;
}
  • 接口注释
    • @ApiOperation("接口说明")
    • @ApiParam("id号")
@ApiOperation("接口说明")
@GetMapping("/find")
@ResponseBody
public Student findStudentById(@ApiParam("id号")Long id){
    Student student = studentService.selectByPrimaryKey(id);
    return student;
}
    • @Api(value = "/template", tags = {"接口名称"})

    • 方法返回对象的说明

      @ApiResponses(value = {
                  @ApiResponse(code = 200, message = "OK"),
                  @ApiResponse(code = 400, message = "Bad Request"),
                  @ApiResponse(code = 401, message = "Unauthorized"),
                  @ApiResponse(code = 403, message = "Forbidden"),
                  @ApiResponse(code = 404, message = "Not Found"),
                  @ApiResponse(code = 500, message = "Internal Server Error")
          })
      
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值