Spring boot uses springfox swagger to showcase RESTful API doc

Source: Internet
Author: User

New file:

import org.slf4j.logger;import org.slf4j.loggerfactory;import  Org.springframework.boot.bind.relaxedpropertyresolver;import org.springframework.context.environmentaware ;import org.springframework.context.annotation.bean;import  org.springframework.context.annotation.configuration;import org.springframework.core.env.environment; Import org.springframework.http.responseentity;import org.springframework.util.stopwatch;import  springfox.documentation.service.apiinfo;import springfox.documentation.spi.documentationtype;import  springfox.documentation.spring.web.plugins.Docket;import  springfox.documentation.swagger2.annotations.enableswagger2;import static  springfox.documentation.builders.pathselectors.regex;/** * api doc -- springfox  swagger configuration */@Configuration @enableswagger2public class swaggerconfiguration  implements environmentaware { &nbsP;  private final logger log = loggerfactory.getlogger ( Swaggerconfiguration.class);     public static final string default_ include_pattern =  "/api/.*";    private relaxedpropertyresolver  propertyresolver;     @Override     public void setenvironment (environment environment)  {        this.propertyresolver  = new relaxedpropertyresolver (environment,  "swagger.");     }     @Bean     public Docket  Swaggerspringfoxdocket ()  {        log.debug ("Starting  Swagger ");         stopwatch watch = new stopwatch ( );         watch.start ();     &nbSp;   docket swaggerspringmvcplugin = new docket (DocumentationType.SWAGGER_ 2)                 .apiinfo ( Apiinfo ())                 . Genericmodelsubstitutes (Responseentity.class)                  .select ()                  .paths (Regex (Default_include_pattern))  // and by paths                 .build ();         watch.stop ();         Log.debug ("Started swagger in {} ms",  watch.gettotaltimemillis ());         return swaggerspringmvcplugin;    }    private apiinfo  Apiinfo ()  {        return new apiinfo (                  Propertyresolver.getproperty ("title"),                 propertyresolver.getproperty ("description"),                 propertyresolver.getproperty ("version"),                  Propertyresolver.getproperty ("Termsofserviceurl"),                 propertyresolver.getproperty ("Contact"),                 propertyresoLver.getproperty ("license"),                 propertyresolver.getproperty ("Licenseurl")         );     }}

The default_include_pattern should fit into your own interface format.

Maven Dependencies to use:

<springfox.version>2.2.2</springfox.version><dependency> <groupid>io.springfox</ Groupid> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</ Version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId> Springfox-swagger2</artifactid> <version>${springfox.version}</version></dependency>

Visit the following URL to see the effect:

Http://localhost:8080/swagger-ui.html


In addition, you can insert it into your own web page via an iframe:

<iframe src= "swagger-ui.html" frameborder= "0" marginheight= "0" marginwidth= "0" width= "100%" height= "" "Scrolli ng= "Auto" target= ' _top ' ></iframe>


Spring boot uses springfox swagger to showcase RESTful API doc

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.