SpringBoot--Swagger
- swagger的基本介绍:
-
- springboot继承swagger:
-
-
配置swagger:
-
package com.model.cofig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/8/7 13:57 */ @Configuration @EnableSwagger2 //开启swagger public class SwaggerConfig { @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } // 配置swagger的信息=apiInfo private ApiInfo apiInfo(){ Contact contact = new Contact("张紫韩", "https://www.cnblogs.com/zzhAylm/", "171832195@qq.com"); return new ApiInfo("张紫韩的swagger" , "逆水行舟,不进则退" , "version 1.0" , "https://www.cnblogs.com/zzhAylm/" , contact , "Apache 2.0" , "http://www.apache.org/licenses/LICENSE-2.0" , new ArrayList<>()); } }
-
配置API文档的分组 :
-
package com.model.cofig; 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.RequestHandler; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/8/7 13:57 */ @Configuration @EnableSwagger2 //开启swagger public class SwaggerConfig { @Bean public Docket docket1(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("张三");//配置多个分组,每个人会扫描自己的包 } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("李四");//配置多个分组 } @Bean public Docket docket(Environment environment){ // 设置一下swagger旨在dev的运行环境才会使用,其他环境我们不开启swagger Profiles profiles = Profiles.of("dev", "test"); // 判断当前环境使否时dev或test boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("张紫韩") .enable(flag)//是否开启swagger // 可以进行分组每个人都只扫描自己设定的包里的controller .select().apis(RequestHandlerSelectors.basePackage("com.model.controller")).build(); } // 配置swagger的信息=apiInfo private ApiInfo apiInfo(){ Contact contact = new Contact("张紫韩", "https://www.cnblogs.com/zzhAylm/", "171832195@qq.com"); return new ApiInfo("张紫韩的swagger" , "逆水行舟,不进则退" , "version 1.0" , "https://www.cnblogs.com/zzhAylm/" , contact , "Apache 2.0" , "http://www.apache.org/licenses/LICENSE-2.0" , new ArrayList<>()); } }
-
配置实体类和控制类的注解:
-
package com.model.controller; import com.model.pojo.User; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/8/9 21:21 */ @Controller public class IndexController { @ApiOperation("Index控制类") @RequestMapping({"/","/index"}) @ResponseBody public String toIndex(){ return "hello world"; } @ApiOperation("hello控制类") @RequestMapping({"/hello"}) public User hello(@ApiParam("用户实体") User user){ return new User(); } }
-
package com.model.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/8/10 20:56 */ @ApiModel("用户实体类") public class User { @ApiModelProperty("用户名") private String userName; @ApiModelProperty("用户密码") private String password; }
-
-