添加依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.1</version> </dependency>
新建配置文件
@Configuration @EnableOpenApi @EnableKnife4j @Import(BeanValidatorPluginsConfiguration.class) public class SwaggerConfiguration { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } /** * 创建 API 信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("标题") .description("描述") .version("1.0.0") .contact(new Contact("Chihiro", "http://www.ykuee.link", "chiliro@mail.com")) .build(); } }
调试
编写一个简单的RESTful接口,代码示例如下:
@Api(tags = "模块一") @RestController public class IndexController { @ApiImplicitParam(name = "name",value = "姓名",required = true) @ApiOperation(value = "接口描述") @GetMapping("/sayHi") public ResponseEntity<String> sayHi(@RequestParam(value = "name") String name){ return ResponseEntity.ok("Hi:"+name); } }
此时,启动Spring Boot工程,在浏览器中访问:http://localhost:8080/doc.html
Comments | NOTHING