spring boot 集成swagger
程序员文章站
2022-05-03 18:01:24
...
**
spring boot 集成swagger
**
-
创建spring boot 项目
-
导入依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
-
swagger 配置
package com.baiwang.test.swagger.config; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.baiwang.test.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs") .description("我的第一个swagger页面") .termsOfServiceUrl("http://localhost:8827/swagger-ui.html#/") .version("1.0").build(); } }
-
启动类添加扫描
package com.baiwang.test.swagger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages={"com.baiwang.test.swagger"}) public class SwaggerApplication { public static void main(String[] args) { SpringApplication.run(SwaggerApplication.class,args); } }
-
定义controller测试
package com.baiwang.test.swagger.controller; import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.PathVariable; 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.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @RestController @Api(tags = "BookController", description = "BookController | 通过书来测试swagger") @RequestMapping(value = "/books") public class BookController { Map<Integer, Book> books = Collections.synchronizedMap(new HashMap<Integer, Book>()); @ApiOperation(value="创建图书", notes="创建图书") @ApiImplicitParam(name = "book", value = "图书详细实体", required = true, dataType = "Book") @RequestMapping(value="/add", method=RequestMethod.POST) public Book postBook(@RequestBody Book book) { books.put(book.getId(), book); return book; } @ApiOperation(value = "获图书细信息", notes = "根据url的id来获取详细信息") @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "int", paramType = "path") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Book getBook(@PathVariable Integer id) { return books.get(id); } } class Book { private Integer id; private String name; /** * @return the id */ public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Book [id=" + id + ", name=" + name + "]"; } }
-
启动项目
请求 http://localhost:8827/swagger-ui.html#/(yml配置文件设置的端口号8827)
效果如下
推荐阅读
-
Spring Boot 文件上传与下载的示例代码
-
详解Spring Boot Security
-
Spring Boot Security 结合 JWT 实现无状态的分布式API接口
-
解决spring boot 1.5.4 配置多数据源的问题
-
Spring Boot使用Druid连接池的示例代码
-
详解如何在Spring Boot启动后执行指定代码
-
详解Spring Boot 项目启动时执行特定方法
-
Spring Boot自定义配置属性源(PropertySource)
-
spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法
-
spring boot 1.5.4 web容器定制(端口号等修改)方法