Swagger-UI

Swagger-UI

Swagger是啥 ? Swagger 是一个用来生成接口文档的开源框架 。前后端分离的情况下,前端通过后端编写的API文档,了解接口的调用规则,但是传统的接口文档,是由后按程序员编写,是个枯燥的体力活。Swagger解决了这个痛点,根据接口方法上写的相关注解 自动生成接口文档,而且还支持测试。

1、新增依赖

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

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

2、新增配置

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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
@EnableWebMvc
public class SwaggerConfig  implements WebMvcConfigurer {
    
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//接口所在路径
            .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("石河子大学SpringBoot商城")//文档说明
            .version("1.0.0")//文档版本说明
            .build();
    }
    
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
            "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
            "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
        //WebMvcConfigurer.super.addResourceHandlers(registry);
    }
    
    
    
    
}

3、新增注解

@Api 注解 用在控制器类上 @ApiOperation 注解 用在接口方法上

@Api(value = "用户接口",tags = "用户接口")
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    IUserService userService;

    @ApiOperation("用户列表")
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public CommonResult   list(@RequestParam(value = "pageNum",required = false,defaultValue = "1")  Integer  pageNum ,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "10")  Integer  pageSize
                               ){
        List<User>  userList  =  userService.findListByPage(pageNum,pageSize);
        return CommonResult.success("分页查询用户数据成功",userList);

    }

备注:我个人只喜欢使用,@Api和 @ApiOperation 其他注解很少使用,我怕代码太乱,降低可读性

其他注解介绍:

@Api 用在Controller 类上

@ApiOperation 用在方法上

@ApiParam 用在参数上

@ApiImplicitParams、@ApiImplicitParam 用在请求的方法上,包含一组参数说明

@ApiModel、@ApiModelProperty 用在实体类和实体类属性上

4、访问接口文档

输入地址:http://localhost:8080/swagger-ui.html#/ 注意ip地址和端口 换成你自己的环境

springboot+swagger No operations defined in spec!


报错页面如下
原因:swagger的配置类找不到路径
解决办法:
1.将@ComponentScan(basePackages = {"com.example.springboot.config"})中的basePackages改为配置类的包路径
2.再将配置类的.apis(RequestHandlerSelectors.basePackage(""))的basePackage写上controller层的包路径。

报错页面如下
原因:swagger的配置类找不到路径

解决办法:
1.将@ComponentScan(basePackages = {“com.example.springboot.config”})中的basePackages改为配置类的包路径
2.再将配置类的.apis(RequestHandlerSelectors.basePackage(""))的basePackage写上controller层的包路径。
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值