build restfu API online documentation and interface test based on Swagger2
1. Introduction of Swagger2 Dependence
<!--documentation for online generation of restful APIs and test services because guava is used too much, many projects are used, causing jar pack conflicts to eliminate guava dependencies--&
Gt <dependency> <groupId>io.springfox</groupId> <artifactid>springfox-swagger
2</artifactid> <version>${swagger2.version}</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <art
Ifactid>guava</artifactid> </exclusion> <exclusion>
<groupId>org.javassist</groupId> <artifactId>javassist</artifactId>
</exclusion> </exclusions> </dependency> <dependency>
<groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> ,;version>${swagger2.version}</version> <exclusions> <exclusion>
<groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency>
2. Register Swagger2
Import Com.google.common.base.Predicate;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Springfox.documentation.RequestHandler;
Import Springfox.documentation.builders.ApiInfoBuilder;
Import springfox.documentation.builders.PathSelectors;
Import springfox.documentation.builders.RequestHandlerSelectors;
Import Springfox.documentation.service.ApiInfo;
Import Springfox.documentation.service.Contact;
Import Springfox.documentation.spi.DocumentationType;
Import Springfox.documentation.spring.web.plugins.Docket;
Import Springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author Created by Tan Jian on 2018/4/12 0012. Thursday.
9:31. *©all Rights Reserved. */@Configuration @EnableSwagger2 public class Swagger2config {@Bean public Docket Getdocket () {Retu
RN New Docket (documentationtype.swagger_2). Apiinfo (Getapiinfo ()). Select () . APIs (Getapis()). Paths (Pathselectors.any ()). build (); /** * Specifies which packets are exposed to SWAGGER2 * * Private predicate<requesthandler> Getapis () {return Request
Handlerselectors.basepackage ("Com.simply.zuozuo.controller");
/** * Specifies the basic information for the constructed document/private Apiinfo Getapiinfo () {return new Apiinfobuilder () . Title ("Restful Api of Zuozuo"). Contact (GetContact ()). Description ("Based on Swagger2-built rest
FUL API Documentation "). Termsofserviceurl (" "). Version (" 1.0.0 "). Build (); /** * Specifies the author information to build the API/private contact GetContact () {return new contacts ("Online Zuozuo", "HT
tps://blog.csdn.net/qq_15071263 "," m15197447018@gmail.com ");
}
}
3. Load Swagger2 Resources
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Import Org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * @author Created by Tan Jian on 2018/3/30 0030. Friday.
13:20. *©all Rights Reserved. * <p> * <p> * in SpringBoot2.0 and Spring 5.0 webmvcconfigureradapter has been deprecated * <p> * NEW implementation * 1. Inherit Webmvccon Figurationsupport * 2. Implement Webmvcconfigurer Interface * * @Configuration public class Interceptorchainloader extends Webmvcconfigur Ationsupport {@Override public void Addinterceptors (Interceptorregistry registry) {Registry.addinterc
Eptor (New Handleinterceptorimpl ());
Super.addinterceptors (registry); @Override public void Addresourcehandlers (Resourcehandlerregistry registry) {//Load HTML resource reg Istry.addresourcehandler ("/static/**"). Addresourcelocations (" classpath:/static/"); Load the Swagger2 resource Registry.addresourcehandler ("/swagger-ui.html"). Addresourcelocations ("classpath:/meta-inf/
Resources/");
Registry.addresourcehandler ("/webjars/**"). Addresourcelocations ("classpath:/meta-inf/resources/webjars/");
Super.addresourcehandlers (registry);
}
}
4. Visit Swagger2 Home
Http://ip:port/context-path/swagger-ui.html