How to use SwaggerUI in SpringBoot

  • 2021-01-19 22:14:42
  • OfStack

Swagger

Swagger is a language-independent specification and framework for defining service interfaces, mainly used to describe API of RESTful. It focuses on creating excellent documentation and client libraries for API. API, which supports Swagger, generates interactive documentation for API methods, allowing users to discover the capabilities of API by visually experimenting with the request and response, header files, and return code.

swagger is used to define API documents.

Benefits:

Separate development of front and rear ends The API documentation is very clear There is no need to use URL input browser to access Controller when testing The traditional test method of entering URL is cumbersome for passing arguments to post requests (of course, you can use browser plug-ins such as postman). spring- The set of boot is simpler than that of swagger

Embedded SpringBoot SwaggerUI

steps

1. Introduce jar pack


<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. Configure SwaggerConfig based on SpringBoot


@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(" Order center test platform ").description(" Here you can browse all the interfaces of the project and provide relevant testing tools ")
        .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 configuration instructions

WebConfig inherits WebMvcAutoConfigurationAdapter rather than directly inherits WebMvcConfigurerAdapter. Otherwise, the Swagger page will not be available.


@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 page access

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


Related articles: