配置Swagger和注解的使用

配置完成后,下面有注解的使用

添加依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

配置Swagger swaggerconfig

放到config里面
在这里插入图片描述

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 
public class SwaggerConfig {
    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_12)
                .apiInfo(apiInfo())
                .enable(true)//enable是否启用swagger ,如果为false , 则swagger不能再浏览器中访问
                .select()
                //RequestHandlerSelectors , 配置扫描接口的方式
                //basepackage:指定要扫描的包
                //any():全部扫描
                //none():不扫描
                //withClassAnnotation():扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation():扫描方法上的注解
                .apis(RequestHandlerSelectors.any())
                //paths():过滤什么路径
                //.paths(PathSelectors.ant("msfh/**"))
                .build()
                ;
    }

    //配置swagger的信息 = apiinfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("致橡树丶", "https://blog.csdn.net/qq_45777315", "412798556@qq.com");
        return  new ApiInfo(
                "zyh的Swagger API文档",
                "好好学习 天天向上",
                "1.0",
                "https://blog.csdn.net/qq_45777315",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList<VendorExtension>());

    }
}

在本地测试

localhost:8080/swagger-ui.html

注解的使用

这里是详细的使用,我是改简单概括下
https://blog.csdn.net/Booleaning/article/details/88829142

在方法里的注解

//controller的一个类上,标志这个类是swagger资源
@Api(value = "说明干啥的")

//作用于方法之上,用户表示一个http的请求
@ApiOperation(value = "方法描述", notes = "方法详情描述")

//用于参数字段的说明
@ApiParam(name = "参数名". value= "参数说明", required = "是否必须")
```在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200615103316686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Nzc3MzE1,size_16,color_FFFFFF,t_70)

## 在实体里面的注解

```java
//作用于类,对类进行说明,用于实体类接收或者返回
@ApiModel(value = "对象名", description = "对象描述")

//作用于实体类,用于实体类中某个字段
@ApiModelProperty(value = "参数解释",name = "参数名",required = "是否必填")

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值