Swagger3的部署和应用

1.添加swagger3maven依赖

  <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
 </dependency>


2.加入配置类

package cn.zlj.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;



@Configuration
@EnableOpenApi
public class SwaggerConfig {
    /**
     * 用于读取配置文件 application.properties 中 swagger 属性是否开启
     */
    @Value("${swagger.enabled}")
    Boolean swaggerEnabled;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启swagger
                .enable(swaggerEnabled)
                .select()
                // 过滤条件,扫描指定路径下的文件
                .apis(RequestHandlerSelectors.basePackage("cn.zlj"))
                // 指定路径处理,PathSelectors.any()代表不过滤任何路径
                //.paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        /*作者信息*/
        Contact contact = new Contact("zlj", "xx", "xx");
        return new ApiInfo(
                "Spring Boot 集成 Swagger3 测试",
                "Spring Boot 集成 Swagger3 测试接口文档",
                "v1.0",
                "xx",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

要在yml文件中设置

swagger:
  enabled: true

3.将注解配置到类上

@Api(tags = {"用户接口"})
@Slf4j
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //增加 save post success
    @PostMapping("/save")
    @ApiOperation("增加")
    public AjaxResult save(@ApiParam(value = "用户对象", required = true) @RequestBody User user){

                 userService.save(user);
        return AjaxResult.me().success();
    }
    //删除 del del

    //修改 update put
    @PutMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody User user){
        userService.update(user);

        return AjaxResult.me().success();
    }
    //查询 getAll get
    @GetMapping("/get")
    @ApiOperation("查询")
    public AjaxResult get(){
        return AjaxResult.me().success(userService.get());
    }
    //分页查询getPage get

    //模糊查询getLike get

}

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "User", description = "用户实体类")
public class User {
    @ApiModelProperty(value = "用户id")
	private Long id;
   @ApiModelProperty(value = "用户名")
	private String username;

	private String phone;

	private String email;

	/** 盐值 */
	private String salt;

	/** 密码,md5加密加盐 */
	private String password;

	/** 员工状态 1启用,0禁用 */
	private Long state;

	private Long age;

	private Date createtime;

	private String headImg;

	private Long logininfoId;

}

然后启动 http://localhost:端口号/swagger-ui/index.html

如果启动失败可以加上一下配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值