shuiluobu 2020-01-07
作用:
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- jackson相关依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>classmate</artifactId> <version>1.2.0</version> </dependency> 2、使用注解来进行启动swagger import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RestController; 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; /** * @author Administrator */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 //.apis(RequestHandlerSelectors.basePackage("com.template.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); // return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } /** * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个 * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("SpringBoot 使用 Swagger2 构建RESTful API") //创建人 .contact(new Contact("Test", "http://www.xuanqi.co/", "")) //版本号 .version("1.0") //描述 .description("API帮助文档") .build(); } }
@Slf4j @RestController @Api(description = "测试接口") @RequestMapping("test") public class TestController extends BaseController { @ApiOperation(value = "测试方法",notes = "返回多个测试结果") @ApiImplicitParam(name="param",value="测试参数",paramType ="查询",required = true, dataType = "String") @GetMapping("constant") public String getConstant(String param){ Map<String,String> map=new HashMap<>(); map.put("threadName",Thread.currentThread().getName()); map.put("param",param); log.info(JsonUtil.toJson(map)); return JsonUtil.toJson(map); } }
http://localhost:9001/web/swagger-ui.html