标签:uil err location span 部署 pen config color string
1 Gradle配置
在dependencies中添加以下依赖:
implementation("io.springfox:springfox-swagger2:2.7.0")
implementation("io.springfox:springfox-swagger-ui:2.7.0")
具体的版本可以在https://mvnrepository.com/artifact/io.springfox中查看到
2 添加Swagger2配置类
package com.learning.test; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; 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; @Configuration @EnableWebMvc @EnableSwagger2 public class Swagger2Configuration implements WebMvcConfigurer { //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.learning.test.controller")) .paths(PathSelectors.any()) .build(); } // 构建api文档的详细信息 private ApiInfo apiInfo() { return new ApiInfoBuilder() // 页面标题 .title("API接口说明") // 创建人 .contact(new Contact("lasdaybg", "", "")) // 版本号 .version("") // 描述 .description("") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
3 Controller示例
package com.learning.test.controller; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.learning.test.model.MyObject; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; @RestController @RequestMapping(value = "/test") @Api("测试接口") public class TestController { @RequestMapping(method = RequestMethod.GET) @ApiOperation(value = "查询对象") @ApiImplicitParams({ @ApiImplicitParam(name = "param1", value = "入参1"), @ApiImplicitParam(name = "param2", value = "入参2")}) public MyObject get(@RequestParam(required = false) String param1, @RequestParam(required = false) String param2) { return new MyObject(); } @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiOperation(value = "创建对象") public void create(@RequestBody(required = false) MyObject myObject) {} } MyObject的类声明如下: package com.learning.test.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel(value = "MyObject", description = "数据模型") public class MyObject { @ApiModelProperty(value = "名称") private String name; @ApiModelProperty(value = "参数1") private Integer param1; @ApiModelProperty(value = "参数2") private Boolean param2; }
这里用到了几类注解:
@Api
用在类上,说明这个是Swagger的资源
@ApiOperation
用在方法上,对方法功能做一个说明
@ApiImplicitParams,ApiImplicitParam
用在方法上,对方法的入参进行说明
@ApiModel
用在模型对象上,对对象的属性等进行说明
另外,这里用到了lombok,具体用法请自行百度。
4 查看swagger文档
启动程序,在浏览器中输入http://localhost:8080/swagger-ui.html,即可看到swagger的文档说明
SpringBoot中部署Swagger2和Swagger-UI
标签:uil err location span 部署 pen config color string
原文地址:https://www.cnblogs.com/lasdaybg/p/10031646.html