SpringBoot和swagger2整合

SpringBoot和swagger2整合学习记录

一.引入swagger2相关依赖

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

二.加入swagger2配置类

package spring.boot.base.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.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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("spring.boot"))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("janis Manage Swagger RESTful APIs")
                .description("自我整合最强大 Swagger API 服务")
                .termsOfServiceUrl("http://swagger.io/")
                .contact(new Contact("janis", "127.0.0.1", "[email protected]"))
                .version("1.0")
                .build();
    }
}

此处需要更改basePackage(“spring.boot”).
"spring.boot"是我项目中的基包,只有更换成你当前项目的基包时,启动在启动的时候才能对该包下进行扫描查询swagger的注解,将接口类和接口方法生成API,展示在swagger-ui页面上;

三.启动类处配置swagger注解

package spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 基础模块的启动类
 * @author pengchen
 * @date 2018-12-02:下午 9:57
 */
@SpringBootApplication
@EnableSwagger2
public class BaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(BaseApplication.class);
    }

}

此处添加的注解@EnableSwagger2

四.controller类和方法加入swagger注解

package spring.boot.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.boot.dto.ResultDTO;
import spring.boot.dto.StatusCode;
import spring.boot.po.Label;
import spring.boot.service.LabelService;

import java.util.List;

/**
 * @author pengchen
 * @date 2018-12-08:下午 4:45
 */
@Api("标签")
@RestController
@RequestMapping("label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 添加一个
     * @param label
     * @return
     */
    @ApiOperation("添加一个标签")
    @PostMapping
    public ResultDTO add(@RequestBody Label label){
        labelService.saveLabel(label);
        return new ResultDTO(true,StatusCode.OK,"添加成功",null);
    }

@Api:

作用在类上,用来标注该类具体实现内容。表示标识这个类是swagger的资源 。
参数:

  1. tags:可以使用tags()允许您为操作设置多个标签的属性,而不是使用该属性。
  2. description:可描述描述该类作用。

@ApiOperation:

用于方法,表示一个http请求的操作 。

猜你喜欢

转载自blog.csdn.net/p778970202/article/details/85014566