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>

二、配置文件config

package com.offcn.springrestfuldemo001.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2   //开启在线文档
public class SwaggerConfig {
    //1.声明api 文档的属性 构建器
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("springboot中使用在线文档构建RestFul风格 Apis")
                .description("小demo").termsOfServiceUrl("http://baidu.com/")
                .contact("java")
                .version("1.0").build();
    }

    //2.配置核心配置信息
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.basePackage("com.offcn.springrestfuldemo001.controller"))
                .paths(PathSelectors.any()).build();
    }

}

三、编写controller

 

  通过@ApiOperation注释来给API增加说明

 

    使用在Api类的接口方法上,主要属性有value(接口名称)、notes(注释)、hidden(是否隐藏)、httpMethod、ignoreJsonView、response、responseHeaders等等,某些属性注解可自动识别,无需配置。

 

  通过@ApiImplicitParams、@ApiImplicitParam注释来给参数增加说明

 

    使用在Api类的接口方法上,对接口参数进行说明,@ApiImplicitParams只有一个属性value,@ApiImplicitParam主要属性有name(参数名称)、value(参数说明)、required(是否必需)、dataType(数据类型)、paramType(参数类型)、readOnly(只读)等。

 

 

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/users-test")
public class UserController {

    private List<User> listUser= Collections.synchronizedList(new ArrayList<User>());

    /**
     * 更新指定id用户信息
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新指定id用户信息",notes = "根据id更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "user",value = "用户实体user",required = true,dataType = "User")
    })

    public String updateUser(@PathVariable("id") Long id,User user) {
        for (User user2 : listUser) {
            if(user2.getId()==id) {
                user2.setName(user.getName());
                user2.setAge(user.getAge());
            }
        }
        return "success";
    }

    /***
     * 删除指定id用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除指定id的用户信息",notes = "根据id删除用户信息")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long")
    public String deleteUser(@PathVariable("id") Long id) {

        listUser.remove(getUser(id));
        return "success";

    }
}

 

四、主启动类

  启动应用,然后访问地址:http://localhost:8080/swagger-ui.html

 

package com.offcn.springrestfuldemo001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringRestfulDemo001Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringRestfulDemo001Application.class, args);
    }

}

 

 

上一篇:SpringBoot学习(三)—— springboot快速整合swagger文档


下一篇:SSM接口可视化