springboot 集成swagger2

一.引入swagger的jar包



    io.springfox
    springfox-swagger-ui
    2.9.2


    io.springfox
    springfox-swagger2
    2.9.2

二.配置配置文件

springboot 集成swagger2_第1张图片

​
package com.ym.eat.config;

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

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //文档说明
                .title("whatToEat")
                //文档版本说明
                .version("1")
                .build();
    }


}

三.配置controller层,配上注解:@RestController,@RequestMapping,@Api,@ApiOperation("say hello")

package com.ym.eat.controller;

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

@RestController
@RequestMapping("/menu")
@Api(value = "menu",tags = "menu")
public class MenuController {

    @GetMapping("/hello")
    @ApiOperation("say hello")
    public String getHello() {
        return "hello";
    }

}

四.启动程序后,在确保程序已启动成功的前提下,打开swagger的ui界面,地址为:http://localhost:8080/swagger-ui.html#/  

打不开的可以试试https,或者查看一下程序端口是否为8080

springboot 集成swagger2_第2张图片

 

参考文章:

https://blog.csdn.net/weixin_42165041/article/details/81077100

swagger的描述注释配置:https://blog.csdn.net/zhongzk69/article/details/94987568

swagger的使用:https://www.jianshu.com/p/349e130e40d5

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