在SpringBoot 中集成Swagger

前提:我的SpringBoot 项目的版本是 



    org.springframework.boot

    spring-boot-starter-parent

    2.7.18-SNAPSHOT

     

一、导入jar包 



    io.springfox

    springfox-boot-starter

    3.0.0

二、编写SwaggerConfig 配置类

代码: 

package com.hjxjpa.springbootjpa.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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@EnableOpenApi
@Configuration
public class SwaggerConfig {
	
	@Bean
	Docket createRestApi() {
		return new Docket(DocumentationType.OAS_30)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.hjxjpa.springbootjpa.web"))
				.paths(PathSelectors.any())
				.build();
	}
	
	private ApiInfo apiInfo() {
		System.out.println("apiInfo已经被执行了..................");
		return new ApiInfoBuilder()
				.title("工作平台API ")
				.description("工作平台SwaggerAPI管理")
				.termsOfServiceUrl("htpp://127.0.0.1/")
				.version("1.0")
				.build();
	}

}

三、在controller类中添加注解

代码:

package com.hjxjpa.springbootjpa.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hjxjpa.springbootjpa.entity.TempUserEntity;
import com.hjxjpa.springbootjpa.service.TempUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "Api-用户管理")
@RestController
@RequestMapping("/api/tempUser")
public class TempUserController {
    @Autowired
    TempUserService tempUserService;
    
    @ApiOperation(value = "用户列表",notes = "用户管理列表")
    @GetMapping("/user")
    public List getUsers(){
        return tempUserService.getUsers();
    }

}

四、配置mvc 路径访问策略

在配置文件中加入以下配置,否则看不到接口内容:

spring.mvc.pathmatch.matching-strategy=ant-path-matcher 

五、查看效果

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

在SpringBoot 中集成Swagger_第1张图片

以上就是swagger集成到springboot项目的基本操作,希望对伙伴们有帮助 !

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