Objective:
In the previous project with nearly a year of Rap,rap is open source from Ali, very useful. GitHub Address: Https://github.com/thx/RAP.
When using this tool, the project members need to be in the interface document after the change, send mail to the project team members, because the rap did not have this function, so also download the source code, increased the e-mail function.
Commit this function code to the community, as if the community did not accept the inclusion.
After using it for a year later, it was found that swagger seemed to be easy to use, directly combined with the code, and needed to be reused with other tools.
So the following are the spring integration notes for swagger.
Swagger Integrated Spring Description:
If it is in the Springboot integrated swagger is very convenient and simple, on-line search, or to the official website a look on the understanding, here is not repeat.
The following are mainly self-integration to the SPRINGMVC of the transformation point:
1, POM.XM file, need to add the following dependencies:
Version is:
<jackson-version>2.5.0</jackson-version>
<swagger-springmvc-version>1.0.2</swagger-springmvc-version>
The increased dependency is, for:
<!--automatically generate interface documentation tools --
<dependency>
<groupId>io.springfox</groupId>
<artifactid>springfox-swagger2</artifactid>
<version>${swagger-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactid>springfox-swagger-UI</Artifactid>
<version>${swagger-version}</version>
</dependency>
<!--automatically generate interface documentation tools --
<!--integrated Swagger2 needs to be used in general Fastjson, but swagger is used by Jackson---
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactid>jackson-annotations</artifactid>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactid>Jackson-DataBind</artifactid>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactid>jackson-core</artifactid>
<version>${jackson-version}</version>
</dependency>
<!--integrated Swagger2 needs to be used --
2. Add a swagger configuration file:
Import Org.springframework.context.annotation.Bean;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
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.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;
/**
* Configuration of the Swagger Interface auto-generation tool
*
* @author Liuhangjun
* @date-01-22
*/
@Configuration
@EnableWebMvc
@EnableSwagger2
@Componentscan ("Com.gradven.portalapi.controller")
Public class Swaggerconfig extends Webmvcconfigureradapter {
@Bean
Public Docket API () {
return new Docket (documentationtype.swagger_2). Select ()
. APIs (Requesthandlerselectors.basepackage ("Com.gradven.portalapi.controller")). Build ()
. Apiinfo (Apiinfo ());
}
private Apiinfo Apiinfo () {
return new Apiinfobuilder ()
//page title
. Title ("API Portal uses Swagger2 to build restful APIs")
//creator
. Contact ( "Gradven", null, "[email protected]" )
//version number
. Version ("1.0")
//Description
. Description ("Portal Interface"). Build ();
}
@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/");
}
}
3. Inject the above class into the spring configuration file:
<!--swagger Configuration--
<Bean class= "Com.gradven.portalapi.commons.config.SwaggerConfig"/>
4. Write a controller to access swagger:
As long as the controller is written under the Com.gradven.portalapi.controller package, you can see the interface again by visiting http://localhost:port/swagger-ui.htm.
About API Interface Documentation rap and swagger