温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot框架如何整合SwaggerUI

发布时间:2022-02-24 13:38:00 来源:亿速云 阅读:131 作者:iii 栏目:开发技术

这篇文章主要介绍了SpringBoot框架如何整合SwaggerUI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot框架如何整合SwaggerUI文章都会有所收获,下面我们一起来看看吧。

整合swagger进行模块测试

注意事项:为方便SpringBoot更好的整合Swagger,需要专门放置在一个模块中(maven子工程)

创建公共模块,整合swagger,为了所有模块进行使用

common/pom.xml,导入相关的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <scope>provided </scope>
    </dependency>

    <!--mybatis-plus-->
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    <!--lombok用来简化实体类:需要安装lombok插件-->
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    <!--swagger-->
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <artifactId>springfox-swagger-ui</artifactId>
    <!-- redis -->
        <artifactId>spring-boot-starter-data-redis</artifactId>
    <!-- spring2.X集成redis所需common-pool2
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
    </dependency>-->
</dependencies>

在公共模块下在创建一个模块,如service_base

在该模块下创建配置类(需要遵循SpringBoot规范,该代码固定)

package com.xsha.servicebase;

import com.google.common.base.Predicates;
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.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 webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }
    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站标题")
                .description("接口文档的描述信息")
                .version("1.0")
                .contact(new Contact("java", "http://www.baidu.com", "1234567890@qq.com"))
}

使用方式

在其他模块(最好是最外层的)的pom.xml引入上面的模块即可

<dependency>
    <groupId>com.xsha</groupId>
    <artifactId>service_base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在该模块的启动类上添加ComponentScan注解,指定需要扫描的包。例如:@ComponentScan(basePackages={"com.xsha"})

然后启动,访问地址:http://127.0.0.1:8001/swagger-ui.html

统一返回结果的格式(自定义结果)

在公共模块下在创建一个模块,如common-utils

创建一个专门管理状态码的接口

public interface ResultCode {
    //定义两个状态码
    public static int SUCCESS = 20000;
    public static int ERROR = 40000;
}

定义返回格式(较为固定)

package com.xsha.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
// 统一返回结果类
@Data
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();
    // 把构造方法定为私有
    private R() {}
    // 成功静态方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }
    // 失败静态方法
    public static R error() {
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失败");
    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    public R message(String message){
        this.setMessage(message);
    public R code(Integer code){
        this.setCode(code);
    public R data(String key, Object value){
        this.data.put(key, value);
    public R data(Map<String, Object> map){
        this.setData(map);
}

使用方式

在其他模块(最好是最外层的)的pom.xml引入上面的模块即可

<dependency>
    <groupId>com.xsha</groupId>
    <artifactId>common_utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

每次返回的结果的类型必须是自定义的“返回格式”类类型

// please use rest style
// 1.select all teachers data
@ApiOperation(value = "所有数据列表")
@GetMapping("findAll")
public R findAllTeachers() {
    List<EduTeacher> teachers = teacherService.list(null);
    return R.ok().data("results", teachers);
}
// request path mast have variable id
// 2.logically delete teacher
@ApiOperation(value = "逻辑删除数据")
@DeleteMapping("{id}")
public R logicDeleteTeacher(@ApiParam(name="id", value="讲师ID", required = true) @PathVariable String id) {
    boolean flag = teacherService.removeById(id);
    return flag? R.ok(): R.error();
}

最后在swagger中测试即可

关于“SpringBoot框架如何整合SwaggerUI”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot框架如何整合SwaggerUI”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI