千家信息网

SpringBoot框架如何整合SwaggerUI

发表于:2024-10-16 作者:千家信息网编辑
千家信息网最后更新 2024年10月16日,这篇文章主要介绍了SpringBoot框架如何整合SwaggerUI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot框架如何整合SwaggerUI文章都
千家信息网最后更新 2024年10月16日SpringBoot框架如何整合SwaggerUI

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

整合swagger进行模块测试

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

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

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

            org.springframework.boot        spring-boot-starter-web        provided                 com.baomidou        mybatis-plus-boot-starter            org.projectlombok        lombok            io.springfox        springfox-swagger2        springfox-swagger-ui            spring-boot-starter-data-redis    

在公共模块下在创建一个模块,如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@EnableSwagger2public 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引入上面的模块即可

    com.xsha    service_base    0.0.1-SNAPSHOT

在该模块的启动类上添加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;// 统一返回结果类@Datapublic class R {    @ApiModelProperty(value = "是否成功")    private Boolean success;    @ApiModelProperty(value = "返回码")    private Integer code;    @ApiModelProperty(value = "返回消息")    private String message;    @ApiModelProperty(value = "返回数据")    private Map data = new HashMap();    // 把构造方法定为私有    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 map){        this.setData(map);}

使用方式

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

    com.xsha    common_utils    0.0.1-SNAPSHOT

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

// please use rest style// 1.select all teachers data@ApiOperation(value = "所有数据列表")@GetMapping("findAll")public R findAllTeachers() {    List 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"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

0