栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot快速整合swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot快速整合swagger

1.创建一个新的springboot工程并在pom里边加入SpringMVC需要的spring-boot-starter-web依赖与swagger所需依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com.hcc
    swagger26
    0.0.1-SNAPSHOT
    swagger26
    Demo project for Spring Boot
    
        1.8
        2.6.1
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            io.springfox
            springfox-swagger2
            ${springfox.version}
        
        
            io.springfox
            springfox-swagger-ui
            ${springfox.version}
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.编写swagger配置类的代码

package com.hcc.swagger26.config;

import com.google.common.base.Predicates;
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.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 platformApi() {
		return new Docket(documentationType.SWAGGER_2)
				.apiInfo(apiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
				.build();
	}
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("API")
				.description("©2021 Copyright. Powered By hcc.")
			    .version("2.0")
			    .build();
	}
}

3.编写controller代码

package com.hcc.swagger26.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AController {

    @ApiOperation(value = "返回一个数字的三倍")
    @GetMapping("/triple")
    public String triple(Integer id){
        id*=3;
        return id.toString();
    }


}

4.我们已经完成springboot整合swagger了

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号