Web development is now more and more inclined to the front-end separation, front-end use Angularjs,react,vue, etc., deployed on the Nodejs, followed by Springboot release rest services, the front and back to detach. This architecture is flexible and especially suitable for collaborative development of large teams. So the problem is, because the front end is interacting with the backend through the API, how does the interface for the rest API in front and back be defined and communicated? The first thought should be swagger.
So what is the goal of swagger,swagger™ is to define a standard, language-agnostic interface for rest APIs that enables people and computers to discover and understand the capabilities of a variety of services without being able to see the source code or to see the document or to detect it through network traffic. When the service is defined by swagger, the consumer can interact with the remote service through a small amount of implementation logic. Similar to a low-level programming interface, Swagger removes a lot of guesswork when invoking a service. Browse Swagger-spec to learn more about the Swagger project, including additional libraries that support other languages.
So in Springboot, how to integrate with swagger. It's actually very simple, just need to put
The following dependencies are added to the MAVEN project's Pom.xml file, and then a configuration Java class is available. The Springboot automatically applies the swagger and generates the appropriate interface.
@Maven dependent
<dependency>
<groupId>io.springfox</groupId>
<artifactid>springfox-swagger2 </artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</ dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId> springfox-swagger-ui</artifactid>
<version>2.6.1</version>
<scope>compile< /scope>
</dependency>
@ Plus a swagger configuration class to control which package to expose the rest API below has swagger information about the app that needs to be displayed on the Web page.
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import springfox.documentation.builders.PathSelectors;
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 @EnableSwagger2 public class Swaggerconfig {@Bean public Docket Createrestapi () {return n EW Docket (documentationtype.swagger_2). Apiinfo (Apiinfo ()). Select (). APIs
(Requesthandlerselectors.basepackage ("Com.example.controller")). Paths (Pathselectors.any ())
. build (); } private Apiinfo Apiinfo () {apiinfo apiinfo = new Apiinfo ("Example REST API"," Example REST API for service Pack "," 1.0 "," Terms of Service ",
New Contact ("Chancein007 Team", "", ""), "", "" ");
return apiinfo; }
}
The above 2 steps completed, the direct start Springboot, when the springboot start, directly after the site with swagger-ui.html can be accessed, specifically see the picture below, is also amazing.