詳解如何在SpringBoot里使用SwaggerUI

Swagger

我們提供的服務(wù)有:成都網(wǎng)站制作、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、隰縣ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的隰縣網(wǎng)站制作公司

Swagger是一種和語言無關(guān)的規(guī)范和框架,用于定義服務(wù)接口,主要用于描述RESTful的API。它專注于為API創(chuàng)建優(yōu)秀的文檔和客戶端庫。支持Swagger的API可以為API方法生成交互式的文檔,讓用戶可以通過以可視化的方式試驗,查看請求和響應(yīng)、頭文件和返回代碼,從而發(fā)現(xiàn)API的功能。

swagger用于定義API文檔。

好處:

  1. 前后端分離開發(fā)
  2. API文檔非常明確
  3. 測試的時候不需要再使用URL輸入瀏覽器的方式來訪問Controller
  4. 傳統(tǒng)的輸入URL的測試方式對于post請求的傳參比較麻煩(當(dāng)然,可以使用postman這樣的瀏覽器插件)
  5. spring-boot與swagger的集比較成簡單

SpringBoot嵌入SwaggerUI

步驟

1.jar包引入

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.2.2</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.2.2</version>
  <scope>compile</scope>
</dependency>

2.基于SpringBoot配置SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket newsApi() {
    //return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(PathSelectors.any()).build();
    Docket docket = new Docket(DocumentationType.SWAGGER_2);
    docket.enable(true);
    docket.apiInfo(apiInfo()).select().paths(PathSelectors.any()).build();
    return docket;
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("訂單中心測試平臺").description("在這里你可以瀏覽項目所有接口,并提供相關(guān)測試工具")
        .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open").contact("test")
        .license("China Red Star Licence Version 1.0").licenseUrl("#").version("1.0").build();
  }
}

3.WebConfig配置說明

這里有一個需要注意的問題,讓W(xué)ebConfig去繼承WebMvcAutoConfigurationAdapter而不是直接繼承WebMvcConfigurerAdapter,否則Swagger的頁面出不來。

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcAutoConfigurationAdapter {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**");
  }

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }

  @Bean
  public Filter characterEncodingFilter() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
  }

  @Bean
  public MappingJackson2HttpMessageConverter converter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    return converter;
  }

  @Bean
  public ViewResolver getViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/jsp");
    resolver.setSuffix(".jsp");
    return resolver;
  }

  @Bean
  public StandardServletMultipartResolver getStandardServletMultipartResolver() {
    return new StandardServletMultipartResolver();
  }
}

4.SwaggerUI頁面訪問

http://localhost:8080/projectName/swagger-ui.html#!/

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞標(biāo)題:詳解如何在SpringBoot里使用SwaggerUI
當(dāng)前鏈接:http://bm7419.com/article30/phdcpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站營銷、面包屑導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)