Springboot集成Swagger文档

本文档介绍了如何在Springboot项目中集成Swagger,通过引入相关依赖并进行配置,实现API接口的文档自动生成。读者将学习到如何开启Swagger服务,并能够通过访问特定URL查看生成的文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入相关依赖

<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>

开启swagger服务,进行详细的配置

package com.kklt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import static com.google.common.base.Predicates.or;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * Created by kklt on 2017/7/13.
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
//设置一个组
    @Bean
    public Docket testApi() {
        return new Docket(DocumentationType.SPRING_WEB)
                .groupName("portalUser")
                .genericModelSubstitutes(DeferredResult.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base,最终调用接口后会和paths拼接在一起
                .select()
                .paths(or(regex("/user/.*")))//需要进行写入文档的接口
                .build()
                .apiInfo(portalUserApiInfo());
    }
//对于一个组头信息的描述
    private ApiInfo portalUserApiInfo() {
        return new ApiInfoBuilder()
                .title("Tesco")//大标题
                .description("前台用户接口")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("NO terms of service")
                .contact(new Contact("李守余", "http://www.baidu.com", "183327xxxx0@163.com"))//作者
                .license("The Apache License, Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值