How to Use SwaggerUI in spring boot,

Source: Internet
Author: User

How to Use SwaggerUI in spring boot,

Swagger

Swagger is a language-independent specification and framework used to define service interfaces and describe RESTful APIs. It focuses on creating excellent documentation and client libraries for APIs. Swagger-enabled APIs can generate interactive documents for API methods, allowing users to experiment visually, view requests and responses, header files, and return code to discover API functions.

Swagger is used to define API documentation.

Benefits:

  1. Frontend and backend separation Development
  2. API documentation is very clear
  3. You do not need to enter the browser URL to access the Controller during the test.
  4. The traditional method of testing input URLs is inconvenient for passing parameters in post requests (of course, browser plug-ins such as postman can be used)
  5. The set of spring-boot and swagger is relatively simple.

SpringBoot embedded SwaggerUI

Procedure

1. Import the jar package

<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 @ EnableSwagger2public class SwaggerConfig {@ Bean public Docket newsApi () {// return new Docket (DocumentationType. SWAGGER_2 ). apiInfo ()). select (). paths (PathSelectors. any ()). build (); Docket docket = new Docket (DocumentationType. SWAGGER_2); docket. enable (true); docket. apiInfo ()). select (). paths (PathSelectors. any ()). build (); return docket;} private ApiInfo apiInfo () {retu Rn new ApiInfoBuilder (). title ("Order Center Test Platform "). description ("here you can browse all the interfaces of the project and provide related test 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

Here is an issue to be aware of. Let WebConfig inherit WebMvcAutoConfigurationAdapter instead of WebMvcConfigurerAdapter directly. Otherwise, the Swagger page will crash.

@Configuration@EnableWebMvcpublic 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. Access the SwaggerUI page

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

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.