Spring Boot 整合 Swagger

Spring Boot 整合 Swagger

生成项目模板

浏览器打开
https://start.spring.io

添加相关依赖

web 相关


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

swagger 相关依赖

		<dependency>
			<groupId>io.springfoxgroupId>
			<artifactId>springfox-swagger2artifactId>
			<version>2.9.2version>
		dependency>
		<dependency>
			<groupId>io.springfoxgroupId>
			<artifactId>springfox-swagger-uiartifactId>
			<version>2.9.2version>
		dependency>

添加配置类

package com.example.demo.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;

/**
 * Swagger API文档配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket restfulApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API 文档标题")
                .description("API 文档描述")
                .version("1.0.0")
                .build();
    }
}

添加controller

package com.example.demo.controller;

import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@Api(value = "类描述")
@RestController
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @ApiOperation(value = "初次见面", notes = "世界您好!")
    @GetMapping("/")
    public String home() {
        return "Hello World!";
    }

    @ApiOperation(value = "向你问好", notes = "hello <名字>")
    @GetMapping("/hello")
    public String hello(@ApiParam(value = "打招呼名字", required = true) @RequestParam String name) {
        return "Hello " + name + "!";
    }

    @ApiOperation(value = "查询车辆接口", notes = "此接口描述xxxxxxxxxxxxx
xxxxxxx
值得庆幸的是这儿支持html标签
"
, response = String.class) @ApiImplicitParams({ @ApiImplicitParam(name = "vno", value = "车牌", required = false, dataType = "string", paramType = "query", defaultValue = "辽A12345"), @ApiImplicitParam(name = "page", value = "page", required = false, dataType = "Integer", paramType = "query", defaultValue = "1"), @ApiImplicitParam(name = "count", value = "count", required = false, dataType = "Integer", paramType = "query", defaultValue = "10") }) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful — 请求已完成"), @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"), @ApiResponse(code = 401, message = "未授权客户机访问数据"), @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"), @ApiResponse(code = 500, message = "服务器不能完成请求")} ) @GetMapping("/vehicles") public ModelMap findVehicles(@RequestParam(value = "vno", required = false) String vno, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "count", required = false) Integer count) throws Exception { logger.info("http://localhost:8501/api/v1/vehicles"); logger.info("## {},{},{}", vno, page, count); logger.info("## 请求时间:{}", LocalDate.now()); ModelMap map = new ModelMap(); map.addAttribute("vno", vno); map.addAttribute("page", page); return map; } }

运行测试

默认端口是8080,这里我改成9090
application.yml

server:
  port: 9090

浏览器访问:http://localhost:9090/swagger-ui.html
Spring Boot 整合 Swagger_第1张图片

参考

Spring Boot:整合Swagger文档

spring-boot-swagger2 使用手册

你可能感兴趣的:(Spring,Boot)