SpringBoot集成SwaggerUi(以及启动时遇到的错误)

本文介绍了如何在SpringBoot项目中集成SwaggerUi,包括引入相关依赖、配置SwaggerConfig、编写接口方法以及启动类。同时,文章详细记录了在启动过程中遇到的一个错误:在MyGetMethod类中httpMethod注解写错,原本为"Get",修正为"GET"后问题得到解决。最后,通过访问http://localhost/swagger-ui.html可以查看和测试生成的接口文档。
摘要由CSDN通过智能技术生成

SwaggerUi是一个自动生成接口文档,并且还可以去测试这些接口的东西。

SpringBoot集成SwaggerUi

引入依赖
<properties>
        <swagger.version>2.6.1</swagger.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${
   swagger.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${
   swagger.version}</version>
        </dependency>
    </dependencies>
编写Swagger配置类com.wjh.config.SwaggerConfig
package com.wjh.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.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;

@Configuration   //表示是Swagger的配置类
@EnableSwagger2   //启用Swagger2
public class SwaggerConfig {
   
    @Bean
    public Docket api(){
   
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
   
        return new ApiInfoBuilder().title("我的接口文档")
                .contact(new Contact("wjh", "", "wjh_dan@163.com"))
                .description("这是swaggerUI生成的接口文档")
                .version("1.0.0.0")
                .build();
    }
}
编写接口方法类com.wjh.server.MyMethod
package com.wjh.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值