SpringBoot——SpringBoot整合Swagger[通俗易懂]

2022-08-04 19:08:30 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

1.添加pom依赖

向pom文件中添加依赖

代码语言:javascript复制
 <!-- swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<!-- swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.5.10</version>
    <scope>compile</scope>
</dependency>

2.添加Swagger的配置类

代码如下:

代码语言:javascript复制
 package com.youyou.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.RequestHandlerSelectors;
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 swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }

}

3.在controller中添加swagger注解

代码语言:javascript复制
 package com.youyou.address;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * //TODO 添加类/接口功能描述
 *
 * @author 刘朋
 * <br/>date 2018-09-06
 */

@Api(description = "第一个接口")
@RestController
@RequestMapping("/hello")
public class HelloWorldController {

    @ApiOperation(value = "你好世界" )
    @GetMapping("")
    public String helloWorld(){
        return "世界你好!";
    }

}

4.运行效果

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106471.html原文链接:https://javaforall.cn

0 人点赞