- 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<>()); } }
-
-
-
4.配置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<>()); } }
5. 配置实体类和控制类的注解:
-
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; }
-
-
SpringBoot--Swagger
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
springboot swagger
1.在pom.xml配置文件添加依赖包io.springfoxspringfox-swagger22.2.2io.springfoxspringfox-swagger-ui2.2.22.Swagger2类import org.springframework.context.annotation.Bean;import org.springf
springboot swagger swagger springboot swagger包 swagger pom.xml spring -
SpringBoot--swagger搭建、配置及使用
SpringBoot--swagger搭建、配置及使用 一、 作用: 1. 接口的文档在线自动生成。
swagger spring 描述文件 请求参数 -
swagger教程-springboot整合swagger
一.swagger简介1.swagger是什么? swagger是REST APIs接口文档生成工具,既然是REST接口那么就和Sping Rest 搭上线了,swagger 可以生
swagger Swagger教程 swagger教程 swagger讲解 spring -
SpringBoot增加Swagger
主页:小王叔叔的博客欢迎来访【Java】SpringBoot项目增加Swagger文档
微服务 SpringBoot Swagger SpringMVC