这里主要swagger在springboot,maven使用:
老版本请求地址:http://xxxx/swagger-ui.html
新版本请求地址:http://xxxx/doc.html
*swagger接入方式 *
-
在maven的pom.xml中配已下内容:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
-
添加SwaggerConfig配置和参数
@Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class SwaggerConfig { @Bean public Docket swaggerApi() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(SWAGGER_PATH))// 选择那些路径和api会生成document .paths(PathSelectors.any()) // 对所有路径进行监控 .build() .apiInfo(apiInfo()).forCodeGeneration(true); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(SWAGGER_TITLE) //小程序服务端API接口文档 .description(SWAGGER_DESCRIPTION)//小程序服务端 .termsOfServiceUrl(SWAGGER_SERVICE_URL)//客户端服务地址 .version(SWAGGER_VERSION).build();//版本 } }
3.Application注解配置启动,当然这第三部如果是把swagger当包引入可以不需要
@SpringBootApplication @EnableScheduling @EnableBaseCore @EnableSwagger2 public class DrawApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DrawApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DrawApplication.class); } }
4 代码上接入swagger
@RestController @RequestMapping("/test") @Api(value = "/test/bill",tags = "TestController",description = "测试对接相关接口") public class TestController { @PostMapping("/test1") @ApiOperation(value = "测试", notes = "测试", httpMethod = "POST") @ApiImplicitParams({@ApiImplicitParam(name = "userInfo", value = "用户详情", paramType = "query", dataType="UserInfo", required = false)}) public void test1(UserInfo userInfo){ } 或者: @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query", dataType="Integer", required = false)}) public void test1(Integer userId){ } 介绍常用命令 @Api:类的控制模块 1. tags:设置多个标签的属性 2. description:描述该类作用 @ApiOperation:接口方法上 @ApiImplicitParams:方法上参数说明 @ApiImplicitParam: 参数说明 1. name :参数名。 2. value : 参数的具体意义,作用。 3. required : 参数是否必填。 4. dataType :参数的数据类型。 5. paramType :查询参数类型,这里有几种形式: @ApiResponses:响应说明 @ApiResponse:@ApiResponses中,一个错误的响应信息 @ApiModel:请求体和返回体说明
5.实体上添加注释:
@Data public class UserInfo{ @ApiModelProperty(name = "userId", value = "ID", dataType = "Integer", required = true) private Integer userId; }
效果展示: