Build RESTFU API online documentation and interface test based on Swagger2

Source: Internet
Author: User
Tags documentation reserved
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

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.