SpringBoot学习-(二十六)SpringBoot整合Swagger2

基本步骤:

  • 添加pom文件依赖
  • 创建配置文件
  • 书写api
  • 访问

添加pom文件依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>

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

创建配置文件

package com.ahut.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.builders.RequestHandlerSelectors;
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 java.util.HashMap;

/**
 * @author cheng
 * @className: Swagger2Config
 * @description: swagger2配置类
 * @dateTime 2018/5/7 15:18
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    /**
     * 日志管理
     */
    private Logger log = LoggerFactory.getLogger(Swagger2Config.class);

    /**
     * @description:
     * @author cheng
     * @dateTime 2018/5/10 14:13
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(createApiInfo())
                .pathMapping("/api/v1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ahut.action"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * @description:
     * @author cheng
     * @dateTime 2018/5/10 14:13
     */
    private ApiInfo createApiInfo() {
        return new ApiInfoBuilder().title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多知识请看博客:https://blog.csdn.net/qq_28988969")
                .contact(new Contact("cheng", "https://blog.csdn.net/qq_28988969", "[email protected]"))
                .version("1.0")
                .build();
    }

}

书写api

package com.ahut.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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;

/**
 * @author cheng
 * @className: HelloAction
 * @description:
 * @dateTime 2018/5/7 15:10
 */
@RestController
@RequestMapping(value = "/hello")
//@Api(value = "HelloAction", tags = {"演示接口:HelloAction"}, description = "用于演示的action")
@Api(value = "HelloAction", description = "用于演示的action")
public class HelloAction {

    //@ApiOperation(value = "向指定用户,say hello", tags = {"用于向用户说你好"}, notes = "注意问题")
    @ApiOperation(value = "向指定用户,say hello", notes = "注意问题")
    @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String")
    @GetMapping(value = "/say")
    public String say(String name) {
        return "hello , " + name;
    }

}

访问

此处我设置了服务器端口为8900
http://localhost:8900/swagger-ui.html

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_28988969/article/details/80271235
今日推荐