Swagger

1、pom中添加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>

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

解决:swagger版本使用2.9.2不要用2.2.2

2、创建swagger2配置类

package com.springboot.config;

import io.swagger.annotations.SwaggerDefinition;
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.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;

/**
 * @author  
 * @Project: springboot01-helloworld
 * @Package com.springboot.config
 * @ClassName: Swagger2Config
 * @Description: Swagger2的配置类 
 * @date Date : 2020年04月15日 11:19 
 * @version:
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yozo.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多请关注http://www.baidu.com")
                .termsOfServiceUrl("http://www.baidu.com")
                .contact("sunf")
                .version("1.0")
                .build();
    }


}

@Configuration:表明该文件是一个配置类

@EnableSwagger2:表示开启Swagger

@Bean:产生一个bean,交由spring管理,在方法中定义api的信息,并配置swagger产生作用的api的包路径。

3、编写一个Controller:Spring Boot中使用Swagger2构建RESTful APIs

package com.springboot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author 
 * @Project: springboot01-helloworld
 * @Package com.yozo.springboot.controller
 * @ClassName: TestSwagger2Controller
 * @Description: Swagger2使用方法
 * @date Date : 2020年04月15日 11:43 
 * @version:
 */
@Api
@RestController
@RequestMapping("/swagger2")
public class TestSwagger2Controller {

	@GetMapping("/hello")
    @ApiOperation(value="随便返回字符串", notes="返回hello world!!!")
    public String hello(){
        return "hello world!!!";
    }

}

没有特定需求,@ApiImplicitParams注解可省略,对于pojo参数swagger会自动封装

指定request类型

@RequestMapper中显示的指定RequestMethod类型,否则SawggerUi会默认为全类型皆可访问, API列表中会生成多条项目。
@RequestMapper中显示的指定RequestMethod类型,否则SawggerUi会默认为全类型皆可访问, API列表中会生成多条项目。
在方法中直接使用@GetMapping等注解,指定request类型在方法中直接使用@GetMapping等注解,指定request类型
点击”Excuse“按钮,执行请求,在下方可看到相应返回
点击”Excuse“按钮,执行请求,在下方可看到相应返回

带参数–swagger不能接收参数

解决:@ApiImplicitParam( name = “userNum”, value = “用户编号”, required = true, dataType = “int”),单个参数dataType不能用Integer,swagger识别参数认为int类型
swagger接收参数

@Api:用在请求的类上,表示对类的说明

tags=“说明该类的作用,可以在UI界面上看到的注解”
value=“该参数没什么意义,在UI界面上也看到,所以不需要配置”

@ApiOperation:用在请求的方法上,说明方法的用途、作用

value=“说明方法的用途、作用”
notes=“方法的备注说明”

@ApiImplicitParams:用在请求的方法上,表示一组参数说明

@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)–> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType=“int” ,不能加引用类型
defaultValue:参数的默认值

@ApiResponses:用在请求的方法上,表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类

@ApiModel:用于响应类上,表示一个返回响应数据的信息

(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)

@ApiModelProperty:

用在属性上,描述响应类的属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值