swagger-plus使用

1、引入Maven依赖

  <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>2.0.4</version>
  </dependency>

   <dependency>
       <groupId>cn.weiguangfu</groupId>
       <artifactId>springfox-swagger2-plus</artifactId>
       <version>2.7.0-1</version>
   </dependency>

2、springboot版本使用 2.3.1.RELEASE
3、配置文件,表示开启swagger的增强,例如参数分组等功能

swagger:
  plus:
    enable: true

4、创建swagger配置文件

package com.example.order.config;
import cn.weiguangfu.swagger2.plus.annotation.EnableSwagger2Plus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableSwagger2Plus
public class SwaggerConfig2 {
@Bean
public Docket createRestApi(Environment environment) {
	//开发和测试环境
	Profiles profiles=Profiles.of("dev","test");
	//通过environment.acceptsProfiles()判断是否处在自己设定的环境当中
	boolean swaggerEnabled=environment.acceptsProfiles(profiles);
	return new Docket(DocumentationType.SWAGGER_2)
			.enable(swaggerEnabled)
			.groupName("SIR_XIE")
			.apiInfo(apiInfo())
			.select()
			.apis(
					//                       RequestHandlerSelectors.any() // 所有都暴露
					RequestHandlerSelectors.basePackage("com.example.order.controller") // 指定包位置
			)
			.paths(PathSelectors.any())
			.build();
}
	@Bean
	public Docket createRestApi2(Environment environment) {
		//开发和测试环境
		Profiles profiles=Profiles.of("dev","test");
		//通过environment.acceptsProfiles()判断是否处在自己设定的环境当中
		boolean swaggerEnabled=environment.acceptsProfiles(profiles);
		return new Docket(DocumentationType.SWAGGER_2)
				.enable(swaggerEnabled)
				.groupName("test")
				.apiInfo(apiInfo())
				.select()
				.apis(
						//                       RequestHandlerSelectors.any() // 所有都暴露
						RequestHandlerSelectors.basePackage("com.example.order.test") // 指定包位置
				)
				.paths(PathSelectors.any())
				.build();
	}



	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("图书管理系统")
				.description("spring boot示例接口API")
				.version("1.0.0")
				.build();
	}
}

5、启动类上增加注解

@SpringBootApplication
@MapperScan("com.example.order")
@EnableSwagger2Plus
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

}

6、controller层增加接口注解
要加上requestExecution 和 responseExecution 相关属性,请求参数和响应参数的信息才会展示在swagger文档上,实体中的ApiRequestExclude和ApiResponseExclude属性才会生效

在这里插入代码片package com.example.order.controller;
import cn.weiguangfu.swagger2.plus.annotation.ApiGroup;
import cn.weiguangfu.swagger2.plus.annotation.ApiPlus;
import cn.weiguangfu.swagger2.plus.enums.ApiExecutionEnum;
import com.example.order.entity.Employees;
import com.example.order.entity.User;
import com.example.order.service.TestService;
import com.example.order.valid.ValidGroup;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Api(value = "测试类1",tags = {"台账数据"})
@ApiPlus(value = true)
public class TestController {
    @Autowired
    private TestService testService;
    @GetMapping("/helloworld")
    @ApiOperation(value = "helloworld")
    public String helloworld() {
        return "hello";
    }
     @PostMapping("/getAllEmp")
    @ApiOperation(value = "查询所有员工信息")
    @ApiGroup(groups = ValidGroup.Select.class,requestExecution = ApiExecutionEnum.EXCLUDE,responseExecution = ApiExecutionEnum.EXCLUDE)
    public List<Employees> getAllEmp( @RequestBody Employees e) {
        List<Employees> employees = testService.getEmployees(e);
        return employees;
    }
}

7、实体类上增加注解

package com.example.order.entity;

import java.io.Serializable;
import java.util.Date;

import cn.weiguangfu.swagger2.plus.annotation.*;
import com.example.order.CodedDict;
import com.example.order.annotation.Dict;
import com.example.order.valid.ValidGroup;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * employees
 * @author
 *  @ApiModel 代表实体
 *  @ApiModelProperty 代表 实体属性
 *  @ApiRequestExclude 请求参数排除,需要在controller中的接口方法上增			   
 *  加@ApiGroup注解
 *  并requestExecution = ApiExecutionEnum.EXCLUDE才可生效,例如  @ApiGroup(groups = ValidGroup.Select.class,requestExecution = ApiExecutionEnum.EXCLUDE)
 *  @ApiRequestFieldRequired 请求参数需要,并且可以设置分组(groups),以及是否必填(required)
 */
@Setter
@Getter
@ApiModel("员工")
public class Employees implements Serializable {
    @ApiModelProperty("主键id")
    @ApiRequestExclude(groups = {ValidGroup.Select.class})
    private Integer id;
    @ApiModelProperty(value = "名称",required = true)
    @ApiRequestFieldRequired(groups = ValidGroup.Select.class,required = true)
//    @ApiRequestInclude(groups = {ValidGroup.Select.class})
    private String name;
    @ApiModelProperty(value = "年龄")
//    @ApiRequestInclude(groups = {ValidGroup.Select.class})
    private Integer age;
    @ApiModelProperty(value = "位置")
//    @ApiRequestInclude(groups = {ValidGroup.Select.class})
    private String position;
    @ApiModelProperty(value = "时间")
//    @ApiRequestInclude(groups = {ValidGroup.Select.class})
    private Date hireTime;


    private static final long serialVersionUID = 1L;
}

8、访问

http://IP:端口/doc.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值