一、在项目中引入Swagger依赖
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
二、Swagger配置
1、新建两个类,分别为SwaggerProperties SwaggerConfiguration
2.SwaggerProperties类的内容
package com.example.book.config.swagger.properties; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "swagger") @Data @AllArgsConstructor @NoArgsConstructor public class SwaggerProperties { //标题 private String title; //描述 private String description; //版本 private String version; //作者 private String author; }
3.SwaggerConfiguration配置内容
package com.example.book.config.swagger; import com.example.book.config.swagger.properties.SwaggerProperties; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import javax.annotation.Resource; @Configuration @EnableSwagger2 public class SwaggerConfiguration{ @Resource private SwaggerProperties swaggerProperties; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("接口文档") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build() //是否使用默认响应消息 .useDefaultResponseMessages(false); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(swaggerProperties.getTitle()) .description(swaggerProperties.getDescription()) .contact(new Contact(swaggerProperties.getAuthor(), "", "")) .version(swaggerProperties.getVersion()) .build(); } }
代码的一些解释如下
4.在application.yml中配置title,等
swagger: title: "项目相关服务说明文档" description: "作者很懒,什么也没下" version: "1.0" author: "thd"
三、在Controller中测试Swagger能否正常使用,写一个接口
package com.example.book.controller; import com.example.book.service.IBookService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.awt.print.Book; import java.util.List; @RestController @Api(tags = {"图书管理控制器"}) @RequestMapping("/book") @Slf4j public class BookController { @Resource IBookService bookService; @GetMapping("/getAll") @ApiOperation(value = "获取所有-book",notes = "获取所有-book") public List<Book> getAll(){ return bookService.getAll(); } }
五、访问http://localhost:4399/doc.html,此地址为Swagger自动生成,4399为自己设置的server.port=4399
完成!
标签:springfox,SpringBoot,Swagger,documentation,springframework,book,文档,import,swagge From: https://www.cnblogs.com/tianhuida/p/16645805.html