“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Introduction: Recently, I participated in the project of a second-hand trading platform website. Since it is a multi-person collaboration, I plan to integrate Swagger2 and write interface documents, which is convenient for team members to refer to and test.
The advantage of the Swagger
- Support API automatic generation of synchronous online documents: After using Swagger, you can directly generate documents through code, no longer need to manually write interface documents, very convenient for programmers, can save time to write documents to learn new technologies.
- Provide Web page online testing API: Documentation is not enough, Swagger generates documentation to support online testing. Parameters and format are set, directly in the interface input parameters corresponding value can be online test interface.
Integration of the swagger
The first step:
Introducing POM dependencies:
<! -- Introducing swagger2 dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<! - introduce swaggerui -- -- >
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
Copy the code
Swagger bootstrap-UI swagger bootstrap-UI swagger bootstrap-UI swagger bootstrap-UI
The second step:
Writing configuration classes
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(a){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.jxauflea.controller"))// Scan the packet path of the interface
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(a){
return new ApiInfoBuilder()
.title("Interface Document")// Swagger file title
.description("")// Customize related descriptions
.contact(new Contact("xxx"."http:localhost/doc.html"."[email protected]"))
.version("1.0")// Current version.build(); }}Copy the code
Note: To auto-assemble our Swagger configuration, add the @enablesWagger2 annotation at the top of the configuration class
The third step
To access the page, by address access to documents: http://localhost:8080/doc.html
The final result
Problems in the integration process
An error was reported when the project started
At first, I also searched for this error on various platforms and most of the answers were that Swagger’s POM is too low and needs an upgrade
I’ll leave the solution here, maybe some friends will use it
How to: Upgrade Guava
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1 the jre</version>
</dependency>
Copy the code
However, I tried this method and it didn’t work. After several iterations, I found that the springboot version was the problem. At the beginning, I changed the version to 2.6.0, and then I changed the version to 2.5.4, which perfectly solved the problem.
Method: Lower the SpringBoot version