SpringBoot集成swagger3

发布于:2024-11-29 ⋅ 阅读:(11) ⋅ 点赞:(0)

一、增加依赖
springboot是2.2.13版本

 implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'


二、增加swagger配置

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class Swagger3Config {
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("Swagger3 Test")
                        .version("1.0")
                        .description("This is a sample Spring Boot RESTful service using springdoc-openapi and OpenAPI 3."));
    }
}

三、配置controller方法,增加注解

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "BenefitsInfoQueryController Interface", description = "Operations pertaining to user management")
@RestController
@RequestMapping("/benefits/info-query")
public class BenefitsInfoQueryController {

    @Operation(summary = "查询订购信息", description = "查询订购信息")
    @PostMapping("query-order-info")
    public List<OrderQueryRespVO> getInitDeliveryOrderItems(@RequestBody OrderQueryReqVO condition) {
        List<OrderQueryRespVO> list = new ArrayList<>();
        list.add(OrderQueryRespVO.builder().serviceNumber("511511515").build());
        return list;
    }
}

四、启动服务,访问
 

http://localhost:8080/swagger-ui.html

注意如果应用配置了path则路径为:http://localhost:8080/my-application/swagger-ui.html

server:
  servlet:
    context-path: /my-application
  port: 8080


界面如下图:

访问这个可以获取api文档,json形式,可以用于导出接口等

http://localhost:8080/my-applicationhttp://localhost:8080/