Swagger is a normative and complete framework for generating, describing, invoking, and visualizing RESTful Web services. The overall goal is to have the client and the file system update at the same speed as the server. File methods, parameters and models are tightly integrated into the server-side code, allowing the API to always stay in sync. Swagger making deployment management and using powerful APIs has never been easier.
Here I bring you a simple integration demo first look at the project structure
Here are the basic steps
I. Adding MAVEN dependencies
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.2.2</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.2.2</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
Two. Writing the Swagger configuration class
- @Configuration
- @EnableSwagger2
- Public class Swaggers {
- @Bean
- Public Docket Swaggerspringmvcplugin () {
- Apiinfo apiinfo = new Apiinfo ("Sample of Springboot", "sample of Springboot", null, null, null, null, null);
- Docket Docket = new Docket (documentationtype.swagger_2). Select (). Paths (Regex ("/user/.*")). Build ()
- . Apiinfo (Apiinfo). Usedefaultresponsemessages (false);
- return docket;
- }
- /*private apiinfo apiinfo () {
- return new Apiinfobuilder (). Title ("Test API")
- . Description ("Shian Test API1")
- . Version ("1.0.0")
- . build ();
- }*/
- / * @Bean
- Public Docket Createrestapi () {
- return new Docket (documentationtype.swagger_2)
- . Apiinfo (Apiinfo ())
- . Select ()
- . APIs (Requesthandlerselectors.basepackage ("Com.didispace.web"))
- . Paths (Regex ("/user/.*"))
- . build ();
- }
- */
- }
Of course, it can be implemented in a chained programming way, where I use new
Three. Writing a controller
- @RestController
- @RequestMapping ("/user")
- @Api (value = "Shop")
- Public class Springbootcontroller {
- @ApiOperation (value = "Get HelloWorld", notes = "simple SPRINGMVC Request")
- @RequestMapping ("/")
- String Home () {
- return "HELLO World";
- }
- @ApiOperation (value = "Get A+b", notes = "Add a string of request parameters based on the URL's classno and URL studentname, restful style request")
- @ApiImplicitParams ({@ApiImplicitParam (name = "Classno", value = "class number", Required = true, DataType = c8> "String"),
- })
- @RequestMapping (value = "/class/{classno}/to/{studentname}", method = Requestmethod.get)
- String World (@PathVariable ("Classno") string Classno, @PathVariable ("Studentname") string Studentname) {
- return Classno + "" + studentname;
- }
- }
Four. Writing application loading class
- @SpringBootApplication
- Public class Application {
- public static void Main (string[] args) {
- Springapplication.run (Application. Class,args);
- }
- }
Swagger will default to all controllers in the Requestmapping method generated API out, in fact, we generally only need the standard interface (like the controller method returned to the page we do not need), All you can do is to set the requirements for the method to generate the API as follows.
This function is basically implemented, we can access the address http://localhost:8080/swagger-ui.html/
View the generated API
Simple example of Springboot integration Swagger2