Swagger简介

一、基本概念

1、定义

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。

2、参数说明

(1)
@Api:用在请求的类上,表示对类的说明
tags= “说明该类的作用,可以在UI界面上看到的注解”
value= “该参数没什么意义,在UI界面上也看到,所以不需要配置”

(2)
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value= “说明方法的用途、作用”
notes= “方法的备注说明”

(3)
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方

  • header --> 请求参数的获取: @RequestHeader

  • query --> 请求参数的获取: @RequestParam

  • path(用于restful接口)–> 请求参数的获取: @PathVariable

  • body(不常用)

  • form(不常用)

dataType:参数类型,默认String,其它值dataType= “Integer”
defaultValue:参数的默认值

(4)
@ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如 400
message:信息,例如 “请求参数没填好”
response:抛出异常的类

(5)
@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用 @RequestBody这样的场景,请求参数无法使用 @ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性

3、参数传入

(1)@RequestParam
目的:将请求参数绑定到你控制器的方法参数上
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
• value:参数名
• required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。
• defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
注:常用于GET请求

(2)@RequestBody
目的:将json格式的数据转为java对象
语法:@RequestBody User user
注:常用于POST请求

(3)键值对映射
@RequestParam Map<String, String> map
@RequestBody Map<String, Object> map

4、GET和POST传参区别

(1)GET传送的数据量较小,不能大于2KB;POST传送的数据量较大,一般被默认为不受限制。
(2)GET安全性非常低,POST安全性较高
(3)GET常用于从服务器获取数据,POST常用于向服务器发送数据
(4)GET传输速率更快
(5)GET拼接url,POST传body,GET限制字符串长度

5、实例展示

@RestController 
@Api(tags="用户相关接口")
public class HelloController {
 
    @RequestMapping("/hello")
    @ApiOperation("你好")
    public String hello() {
        return "Hello World6666";
    }
    
    @Autowired
private UserService userService;
 
@RequestMapping(value="/tt",method=RequestMethod.POST)
@ApiOperation("找用户")
@ApiImplicitParams({
@ApiImplicitParam(name="username",value="用户名",defaultValue="李四"),
@ApiImplicitParam(name="address",value="用户地址",defaultValue="深圳",required=true)
})
@ApiResponses({
@ApiResponse(code=400,message="请求参数没填好"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})
public void find(@RequestParam(value="id")int i) {
User user=userService.findUserById(i);
System.out.print(user.toString());
}
 
    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户")
    public String getUserInfo() {
            return "Hello World9999";
    }
    
}

二、具体实现

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.example.config;
 
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class Swagger2Config {
 
 
    /**
     * 创建swagger对象:
     *
     * @return
     */
    @Bean
    public Docket CarApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("API")
                        .pathMapping("/")
                        .select()
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                      .title("标题")
                      .description("定义了多个接口")
                      .contact(new Contact("xx",null ,null))
                      .version("1.0")
                      .build());
    }
    
    
    @Bean
    public Docket mapRestApi2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("组合2")
                        .pathMapping("/")
                        .select()
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                      .title("标题")
                      .description("定义了多个接口")
                      .contact(new Contact("xx",null ,null))
                      .version("1.0")
                      .build());
    }
    
}

3、编写接口

见上文实例展示

4、浏览器搜索

http://localhost:8080/swagger-ui.html#/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值