Swagger2复制黏贴版

Pom.xml

<!--Swagger学习,需要导入2个依赖 -->
		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
		<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>

SwaggerConfig类

@Configuration
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
    @Autowired
   TypeResolver typeResolver;

    //配置了Swagerr的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger环境
        Profiles profiles=Profiles.of("dev");
        //获取项目的环境:environment.acceptsProfiles判断是否处在自己设定的环境中
      boolean flag=  environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启用Swagger,如果为false,则不能在浏览器中访问
                .enable(flag)
                .groupName("冰哥")
                .select()
                //配置要扫描的接口方式
                //basePackage指定要扫描的包
                //any()扫描全部
                //none()都不扫描
                //withClassAnnotation()扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation()扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                //过滤
//                .paths(PathSelectors.ant("/example"))
                .build()//工厂模式
                //导入model实体类
                .additionalModels(typeResolver.resolve(Department.class))
                .additionalModels(typeResolver.resolve(Employee.class));

    }
    @Bean
    public Docket docket2(){
        return  new Docket(DocumentationType.SWAGGER_2).groupName("测试第二个Group");
    }

    //配置Swagger的配置信息
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact=new Contact("张璟冰","Mr.ZJBUrl","610904360@qq.com");
        return  new ApiInfo("张璟冰的API文档",
                "刘静是🐷",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

实体类中

@ApiModel(description="公司实体类")
public class Department {
//    实体类中,Integer类型的属性加@ApiModelProperty时,必须要给example参数赋值,且值必须为数字类型。
    @ApiModelProperty(value = "编号",example = "1")
    private Integer id;
    @ApiModelProperty(value = "部门名称")
    private String DepartmentName;

控制类

 @ApiOperation("添加完成")
    //SpringMvc 自动将请求参数和入参数对象的属性一一绑定;要求请求参数的名字和javaBean入参对象里面的属性名一致
    @PostMapping("/emp")
    public String addEmp(@ApiParam("需要添加的员工")Employee employee){
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值