在Springboot项目中使用Swagger文档

在Springboot项目中使用Swagger文档

使用步骤

1.添加依赖

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

2.添加配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lee.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                // .enable(false)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试swagger2")
                .version("v1.0")
                .description("API 测试文档")
                .build();
    }
}

3.注解接口类

  • tags属性——描述接口信息
@Api(tags = "xxx接口")

4.注解接口方法

@ApiOperation("xxx")
@ApiImplicitParam(paramType = "xxx", name = "xxx", value = "xxx", required = true)

@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1"),
		@ApiImplicitParam(name = "pageSize", value = "页尺寸", defaultValue = "20"),
		@ApiImplicitParam(name = "personName", value = "人员姓名")
})

@ApiIgnore
@ApiParam

5.注解实体类

@ApiModel(value = "xxx", description = "xxx")

@ApiModelProperty(value = "员工id")

6.访问接口文档

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

接口过滤

  • 使用@ApiIgnore
  • 在Docket上增加筛选
    • apis():可以通过指定包名的方式,让 Swagger 只去某些包下面扫描。
    • paths():可以通过筛选 API 的 url 来进行过滤。

生产环境中禁用swagger

  • 取消配置类中下面的注释,重启后再访问接口文档如图所示
// .enable(false) 项目上线时关闭接口文档

在这里插入图片描述

  • 或者直接指定环境
@Profile("dev")

问题1

当多个接口的请求路径相同,靠参数来区分时,swagger ui只能显示任意其中一个接口

在这里插入图片描述

问题2

  • 没有在代码中配置swagger时,会出现以下异常,配置后删除target重新rebuild即可解决
  • 参考博客

在这里插入图片描述

问题3

  • AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
2018-10-24 23:03:36.537  WARN 19699 --- [nio-1111-exec-4] i.s.m.p.AbstractSerializableParameter    : Illegal DefaultValue null for parameter type integer

java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_171]
	at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_171]
	at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_171]
	at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
	at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687) [jackson-databind-2.9.6.jar:2.9.6]
  • 原因:这是由于实体类使用@ApiModelProperty时,example属性没有赋值导致的,在AbstractSerializableParameter的getExample方法中会将数值属性的example的转换数值类返回,example的默认值是"",因此当example没有赋值时,会出现上面的异常。getExample方法如下

        @JsonProperty("x-example")
        public Object getExample() {
            if (example == null) {
                return null;
            }
            try {
                if (BaseIntegerProperty.TYPE.equals(type)) {
                    return Long.valueOf(example);
                } else if (DecimalProperty.TYPE.equals(type)) {
                    return Double.valueOf(example);
                } else if (BooleanProperty.TYPE.equals(type)) {
                    if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
                        return Boolean.valueOf(example);
                    }
                }
            } catch (NumberFormatException e) {
                LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
            }
            return example;
        }
    
  • 解决方法:将每一个数值类型上@ApiModelProperty的example都赋值数字字符串即可

  • 参考文章:https://blog.csdn.net/weixin_38229356/article/details/83353347

问题4

  • 访问接口文档遇到404

  • 可能的原因:跨域

  • 解决方法:https://blog.csdn.net/xtj332/article/details/80595768

  • 试试用127.0.0.1代替localhost

参考博客

  • https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html

  • https://blog.csdn.net/itguangit/article/details/78978296

  • https://dzone.com/articles/spring-boot-2-restful-api-documentation-with-swagg

  • https://dzone.com/articles/spring-boot-restful-api-documentation-with-swagger

  • https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值