Framework configuration for swagger2 API documentation

      With the development of Internet technology, the current website architecture has basically changed from the original back-end rendering to the form of front-end rendering and separation of front-end and back-end, and front-end technology and back-end technology are going further and further on their own roads.
       The only connection between the front-end and the back-end has become an API interface; API documentation has become the link between front-end and back-end developers, and it has become more and more important. It swaggeris a framework that allows you to better write API documentation.

pom.xml add dependencies

<!-- Restful API接口说明 生成  swagger  -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.5.0</version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.5.0</version>
   <scope>compile</scope>
</dependency>


controller content

@RequestMapping ( value = "/per/{id}" , method = RequestMethod.GET )
 @ApiOperation ( value = "Test" , notes = "Test Reverse Engineering" )
 public ResultVO selectByPrimaryKey ( @PathVariable ( " id" )Integer id ) {

   per perLIst = userservice .selectByPrimaryKey (id) ;
   return ResultVOUtil. success (perLIst) ;
}



swagger2 file content

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurerAdapter {


   @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

   
      registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
      
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
   }

   @Bean
public Docket createRestApi(){
      return  new Docket(DocumentationType.SWAGGER_2)   
            .apiInfo (apiInfo ())
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build();
   }


   private ApiInfo apiInfo(){
      return new ApiInfoBuilder()
            .title( "Document" )
            .description( "Service interface provided to external systems for management business." )
            .termsOfServiceUrl("http://terms-of-services.url")
            .version("1.0")
            .build();
   }
}

Access address: http://localhost:8085/swagger-ui.html



It is very convenient to test and try it out!

My demo uses JSON to pass data, use RESTful style get query post create put update del delete can be tested

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901892&siteId=291194637