欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot框架整合SwaggerUI的示例代碼

 更新時(shí)間:2022年02月24日 10:52:48   作者:xsha_h  
項(xiàng)目中使用了很多現(xiàn)成的框架,都是項(xiàng)目經(jīng)理、架構(gòu)師帶來(lái)的,從來(lái)沒(méi)有自己整合過(guò),今天給大家介紹下SpringBoot框架整合SwaggerUI的過(guò)程,感興趣的朋友跟隨小編一起看看吧

整合swagger進(jìn)行模塊測(cè)試

注意事項(xiàng):為方便SpringBoot更好的整合Swagger,需要專門(mén)放置在一個(gè)模塊中(maven子工程)

創(chuàng)建公共模塊,整合swagger,為了所有模塊進(jìn)行使用

common/pom.xml,導(dǎo)入相關(guān)的依賴

<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用來(lái)簡(jiǎn)化實(shí)體類:需要安裝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>

在公共模塊下在創(chuàng)建一個(gè)模塊,如service_base

在該模塊下創(chuàng)建配置類(需要遵循SpringBoot規(guī)范,該代碼固定)

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("網(wǎng)站標(biāo)題")
                .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>

在該模塊的啟動(dòng)類上添加ComponentScan注解,指定需要掃描的包。例如:@ComponentScan(basePackages={"com.xsha"})

然后啟動(dòng),訪問(wèn)地址:http://127.0.0.1:8001/swagger-ui.html

統(tǒng)一返回結(jié)果的格式(自定義結(jié)果)

在公共模塊下在創(chuàng)建一個(gè)模塊,如common-utils

創(chuàng)建一個(gè)專門(mén)管理狀態(tài)碼的接口

public interface ResultCode {
    //定義兩個(gè)狀態(tài)碼
    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;
// 統(tǒng)一返回結(jié)果類
@Data
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回碼")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回?cái)?shù)據(jù)")
    private Map<String, Object> data = new HashMap<String, Object>();
    // 把構(gòu)造方法定為私有
    private R() {}
    // 成功靜態(tài)方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }
    // 失敗靜態(tài)方法
    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>

每次返回的結(jié)果的類型必須是自定義的“返回格式”類類型

// please use rest style
// 1.select all teachers data
@ApiOperation(value = "所有數(shù)據(jù)列表")
@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 = "邏輯刪除數(shù)據(jù)")
@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中測(cè)試即可

到此這篇關(guān)于SpringBoot框架整合SwaggerUI的文章就介紹到這了,更多相關(guān)SpringBoot整合SwaggerUI內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 新的Java訪問(wèn)mysql數(shù)據(jù)庫(kù)工具類的操作代碼

    新的Java訪問(wèn)mysql數(shù)據(jù)庫(kù)工具類的操作代碼

    本文通過(guò)實(shí)例代碼給大家介紹新的Java訪問(wèn)mysql數(shù)據(jù)庫(kù)工具類的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-12-12
  • springcloud引入spring-cloud-starter-openfeign失敗的解決

    springcloud引入spring-cloud-starter-openfeign失敗的解決

    這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 解析Spring Cloud Bus消息總線

    解析Spring Cloud Bus消息總線

    這篇文章主要介紹了Spring Cloud Bus消息總線的介紹及使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • SpringBoot參數(shù)校驗(yàn)之@Valid的使用詳解

    SpringBoot參數(shù)校驗(yàn)之@Valid的使用詳解

    這篇文章主要通過(guò)示例為大家詳細(xì)介紹一下介紹了SpringBoot參數(shù)校驗(yàn)中@Valid的使用方法,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-06-06
  • SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作的示例

    SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作的示例

    這篇文章主要介紹了SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Java數(shù)字圖像處理之圖像灰度處理

    Java數(shù)字圖像處理之圖像灰度處理

    這篇文章主要為大家詳細(xì)介紹了Java數(shù)字圖像處理之圖像灰度處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Spring中Cache的使用方法詳解

    Spring中Cache的使用方法詳解

    這篇文章主要介紹了Spring中Cache的使用方法詳解,Spring Cache 是一個(gè)框架,實(shí)現(xiàn)了基于注解的緩存功能,只需要簡(jiǎn)單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能,Spring Cache 提供了一層抽象,底層可以切換不同的緩存實(shí)現(xiàn),需要的朋友可以參考下
    2024-01-01
  • 一篇文章帶你入門(mén)Java字面量和常量

    一篇文章帶你入門(mén)Java字面量和常量

    這篇文章主要介紹了探究Java的常量,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建

    Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建

    這篇文章主要介紹了Netty源碼分析NioEventLoop初始化線程選擇器創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • java簡(jiǎn)易文本分割器實(shí)現(xiàn)代碼

    java簡(jiǎn)易文本分割器實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)易文本分割器的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論