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", "1563351134@qq.com"))
                .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

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值