欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot 集成swagger

程序员文章站 2022-05-03 18:01:24
...

**

spring boot 集成swagger

**

  1. 创建spring boot 项目
    spring boot 集成swagger

  2. 导入依赖

     <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>
    
  3. 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();
         }
     }
    
  4. 启动类添加扫描

     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);
     	}
     }
    
  5. 定义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 + "]";
     	}
     	
     }
    
  6. 启动项目
    请求 http://localhost:8827/swagger-ui.html#/(yml配置文件设置的端口号8827)
    效果如下
    spring boot 集成swagger
    spring boot 集成swagger