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

SpringBoot基于Swagger2構(gòu)建API文檔過程解析

 更新時(shí)間:2019年11月06日 08:33:48   作者:我好難啊upup  
這篇文章主要介紹了SpringBoot基于Swagger2構(gòu)建API文檔過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、添加依賴

<!--SpringBoot使用Swagger2構(gòu)建API文檔的依賴-->
    <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>

二、創(chuàng)建Swagger2配置類

package com.offcn.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//表示該類為一個(gè)配置類,相當(dāng)于spring中的xml配置文件
@EnableSwagger2 //開啟在線文檔
public class SwaggerConfig {

  //1.聲明 api 文檔的屬性
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs")
        .description("優(yōu)就業(yè)")
        .termsOfServiceUrl("http://www.ujiuye.com/")
        .contact("小劉同學(xué)")
        .version("1.0")
        .build();
  }

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

三、修改Controller 增加文檔注釋

通過@ApiOperation注解來給API增加說明

通過@ApiImplicitParams@ApiImplicitParam注解來給參數(shù)增加說明

package com.offcn.controller;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/rest")
@RestController
public class RestFulController {

  @Autowired
  private UserDao userDao;

  @GetMapping("/getUserById")
  @ApiOperation(value="查找指定id用戶信息", notes="根據(jù)id查找用戶信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"),
  })
  public User getUserById(Integer id){
    User user = userDao.getOne(id);
    return user;
  }

  @DeleteMapping("/del")
  @ApiOperation(value="刪除指定id用戶信息", notes="根據(jù)id刪除用戶信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"),
  })
  public String delUserById(Integer id){
    userDao.deleteById(id);
    return "success";
  }
}

四、查看Swagger2文檔

重啟項(xiàng)目

訪問:

http://localhost:8080/swagger-ui.html

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java進(jìn)程間通信之消息隊(duì)列

    Java進(jìn)程間通信之消息隊(duì)列

    這篇文章主要為大家詳細(xì)介紹了Java進(jìn)程間通信之消息隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • JAVA?OOM內(nèi)存溢出問題深入解析

    JAVA?OOM內(nèi)存溢出問題深入解析

    這篇文章主要為大家介紹了JAVA?OOM內(nèi)存溢出問題深入解析,在生產(chǎn)環(huán)境搶修中,我們經(jīng)常會(huì)碰到應(yīng)用系統(tǒng)java內(nèi)存OOM的情況,這個(gè)問題非常常見,今天我們就這個(gè)問題來深入學(xué)習(xí)探討一下
    2023-10-10
  • Java垃圾回收finalize()作用詳解

    Java垃圾回收finalize()作用詳解

    這篇文章主要為大家詳細(xì)介紹了Java垃圾回收finalize()作用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Mybatis的詳細(xì)使用教程

    Mybatis的詳細(xì)使用教程

    這篇文章主要介紹了Mybatis的詳細(xì)使用教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • Java?17新特性詳細(xì)講解與代碼實(shí)例

    Java?17新特性詳細(xì)講解與代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java?17新特性詳細(xì)講解與代碼實(shí)例的相關(guān)資料,Java 17是2021年9月發(fā)布的最新版本,其中包含了很多新特性和改進(jìn),這些新特性和改進(jìn)將進(jìn)一步提高 Java 語言的性能和可用性,需要的朋友可以參考下
    2023-09-09
  • MyBatis中${}?和?#{}?有什么區(qū)別小結(jié)

    MyBatis中${}?和?#{}?有什么區(qū)別小結(jié)

    ${}?和?#{}?都是?MyBatis?中用來替換參數(shù)的,它們都可以將用戶傳遞過來的參數(shù),替換到?MyBatis?最終生成的?SQL?中,但它們區(qū)別卻是很大的,今天通過本文介紹下MyBatis中${}?和?#{}?有什么區(qū)別,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Idea中如何調(diào)出Run dashboard 或services窗口

    Idea中如何調(diào)出Run dashboard 或services窗口

    這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)

    SpringBoot使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)

    角色權(quán)限校驗(yàn),是保證接口安全必備的能力:有權(quán)限才可以操作,所以,一般對(duì)于這種通用邏輯,推薦不與主業(yè)務(wù)邏輯耦合,那么怎么來解耦,那么本文小編就給大家詳細(xì)講解如何使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn),需要的朋友可以參考下
    2023-07-07
  • 詳解Spring?延遲初始化遇到的問題

    詳解Spring?延遲初始化遇到的問題

    這篇文章主要介紹了我們?cè)谑褂肧pring延遲初始化容易遇到的問題,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的可以借鑒一下
    2023-05-05

最新評(píng)論