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:
- Frontend and backend separation Development
- API documentation is very clear
- You do not need to enter the browser URL to access the Controller during the test.
- 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)
- 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.