怎样给Swagger换皮肤?

怎样给Swagger换皮肤?

上文咱们讲到在Spring Boot中集成Swagger2的组件,那今天咱们就来聊聊怎样给Swagger换个皮肤呢?环境搭建:使用Spring Boot依赖swagger-spring-boot-starter进行快速构建。具体swagger-spring-boot-starter能够参考:https://github.com/SpringForA... 。构建工具是Maven,开发工具是IDEA,JDK版本是1.8。html

第一步:Maven快速构建Spring Boot的web项目

1543234878600

第二步:解压,IDEA导入项目

1543235052853

第三步:集成swagger-spring-boot-starter

pom中依赖:java

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.8.0.RELEASE</version>
</dependency>

1543235220552

添加@EnableSwagger2Doc添加容许启用swagger注解,默认状况下就能产生全部当前Spring MVC加载的请求映射文档。git

第四步:配置swagger

# 在application.properties进行配置
swagger.title=码歌学院API
swagger.description=码歌学院相关接口API文档
swagger.version=1.1
swagger.base-package=com.mage

其余具体配置请参考GitHub,https://github.com/SpringForA...。注意在IDEA中配置文件存在中文,那么须要将其配置文件的编码设置成utf-8。具体设置:File -> Settings -> Editor -> File Encodings将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上。github

第五步:编写TestController

package com.mage.swagger02.controller;

import com.mage.swagger02.model.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("test")
@Api(tags = "测试API接口")
public class TestController {

    @GetMapping("")
    @ApiOperation(value="获取列表数据", notes="获取列表下测试数据")
    public String list() {
        return "查询列表数据!";
    }

    @GetMapping("{id}")
    @ApiOperation(value="获取ID数据", notes="根据ID获取某条测试数据")
    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)
    public String find(@PathVariable Integer id) {
        return String.format("根据主键查询数据: %d", id);
    }

    @PostMapping("")
    @ApiOperation(value="新增数据")
    @ApiParam(name = "test", value = "添加的测试模型实体")
    public String add(@RequestBody Test test) {
        return "插入数据!";
    }

    @PutMapping("{id}")
    @ApiOperation(value="更新数据", notes="根据ID更新测试数据")
    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)
    public String update(@PathVariable Integer id, @ApiParam(name = "test", value = "更新的测试模型实体") @RequestBody Test test) {
        return String.format("根据主键更新一条记录: %d", id);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value="删除数据", notes="根据ID删除测试数据")
    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)
    public String delete(@PathVariable Integer id) {
        return String.format("根据主键删除记录: %d", id);
    }
}

第六步:启动执行,浏览器输入http://localhost:8080/swagger-ui.html

1543236194060

第七步:换皮肤

你们若是以为swagger这种皮肤很差看,那么能够更换,只须要在pom中引入一下jar包:web

<dependency>
    <groupId>com.github.caspar-chen</groupId>
    <artifactId>swagger-ui-layer</artifactId>
    <version>1.1.2</version>
</dependency>

而后浏览器输入:http://localhost:8080/docs.htmlspring

1543236493872

好了换肤完成,源码下载:https://github.com/magekang/s...浏览器

相关文章
相关标签/搜索