SpringBoot整合Swagger2

Swagger基础快速上手

Swagger:自动生成Restful接口说明文档工具,也可以在前后端分离时,后端进行接口调试

添加Swagger2依赖:

<!--配置文件解析器,实例化SwaggerConfigProperties,并为各个属性赋值 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>          <optional>true</optional>
</dependency>
<!-- Swagger Maven dependency start-->
<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>
<!--end-->

SwaggerConfigProperties

package com.swagger.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix="swagger.config")
public class SwaggerConfigProperties {

    private Boolean enable;

    private String  packageScan;

    private String  title;

    private String  description;

    private String  version;

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public String getPackageScan() {
        return packageScan;
    }

    public void setPackageScan(String packageScan) {
        this.packageScan = packageScan;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

application.properties文件

server.port=8001
swagger.config.enable=true
swagger.config.packageScan=com.swagger.demo.controller
swagger.config.title=Swagger
swagger.config.description=this is description
swagger.config.version=2.6.1

SpringBoot的Swagger2配置类

package com.swagger.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
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;


@Configuration
@EnableSwagger2
@EnableConfigurationProperties({ SwaggerConfigProperties.class })
@ComponentScan(basePackageClasses = SwaggerConfigProperties.class)
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private SwaggerConfigProperties scp;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

           if(scp.getEnable()){
               registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
               registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
           }
        }
    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder().title(scp.getTitle())//大标题
                .description(scp.getDescription()).version(scp.getVersion())//版本
                .build();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).enable(scp.getEnable())
                .useDefaultResponseMessages(false).select()
                .apis(RequestHandlerSelectors.basePackage(scp.getPackageScan())).build();
    }
}

启动应用:访问http://localhost:8001/swagger-ui.html

涉及到的注解:

  1. @Api(description = “Swagger测试类02”):用在类上解释说明。标记一个Controller类做为swagger 文档资源,与Controller注解并列使用。
  2. @ApiOperation(“第二个测试接口02”):用在方法上解释说明
  3. @ApiResponses({ @ApiResponse(code=200,message = “调用成功”),@ApiResponse(code=500,message=“调用失败”)}):用在方法上解释说明响应状态码
  4. Swagger详细注解传送门:

异常总结:
这里写图片描述


原因:控制层的RequestMapping存在重复URL,导致无法建立映射关系;

能力有限;第一次写博客。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值