Swagger is a RESTFUL interface document automatic generation + functional testing function software. This article briefly introduces the methods and some common problems of integrating swagger into your project. If you want to analyze the project source code in depth and learn more, see the reference materials.
Swagger is a standardized and complete framework for generating, describing, calling, and visualizing RESTful-style web services. The overall goal is to make the client and file system update at the same speed as the server. File methods, parameters and models are tightly integrated into server-side code, allowing the API to always keep synchronized. Swagger has never been easier to deploy and manage and use powerful APIs.
For students who move bricks, writing interfaces is easy, writing interface documents is very annoying. If the interface changes, maintaining interface documents is even more annoying, so you can often find that the document does not match the program.
After a while, even the developers were confused
Swagger2 quickly and conveniently solves the above problems. A new favorite who can organize powerful RESTful API documentation with Spring MVC programs.
The following code is directly added
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhongxin.wealth</groupId> <artifactId>wealthweb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wealthweb</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> </dependencies></project>
Create a configuration class
package com.zhongxin.wealth.apiConfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;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;/** * Created by DingYS on 2017/12/8. */@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.zhongxin.wealth.web")) .paths(PathSelectors.any())) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Langfang Public Loan Big Data Statistics Results Output Interface") .version("1.0") .build(); }}Write controller
package com.zhongxin.wealth.web;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by DingYS on 2017/12/7. */@RestController@RequestMapping("/hello")public class HelloWordController { @ApiOperation(value="test interface", notes="This is just an interface to test the controller call, without any business logic") @RequestMapping(value = {"/test"},method = RequestMethod.GET) public String testHello(){ return "hello"; }}The code is completed, and the effect is ready
Click Try it out!
Is it very detailed, very high-end?
Note: The swagger2.2.2 version that you just used during the integration process will have an error reminder on the homepage.
{"schemaValidationMessages":[{"level":"error","message":"Can't read from file http://127.0.0.1:8888/v2/api-docs"}]}However, the browser access: http://127.0.0.1:8888/v2/api-docs can get the results again
{"swagger":"2.0","info":{"version":"1.0","title":"Langfang Commission-Loan Big Data Statistics Results Output Interface","contact":{},"license":{}},"host":"127.0.0.1:8888","basePath":"/","tags":[{"name":"hello-word-controller","description":"Hello Word Controller"}],"paths":{"/hello/test":{"get":{"tags":["hello-word-controller"],"summary":"test interface","description":"This is just an interface to test the controller call, without any business logic","operationId":"testHelloUsingGET","consumes":["appl ication/json”],"produces":["/"],"responses":{"200":{"description":"OK","schema":{"type":"string"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found”}}}}}}}The specific reason is unknown, and it has not appeared after the 2.7.0 version.
Summarize
The above is the example code of SpringBoot integrated swagger introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!