The swagger UI can be used to intercept the controller layer, generate the request API, and display it in the browser. We can view and debug interfaces directly from the browser.
1 adding Maven Dependencies
<!--Swagger - <Dependency> <groupId>Io.springfox</groupId> <Artifactid>Springfox-swagger-ui</Artifactid> <version>2.6.0</version> </Dependency> <Dependency> <groupId>Io.springfox</groupId> <Artifactid>Springfox-swagger2</Artifactid> <version>2.6.0</version> </Dependency>
2 Adding Swagger Configuration classes
PackageCom.springboot.config;Import StaticSpringfox.documentation.builders.PathSelectors.regex;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importspringfox.documentation.builders.RequestHandlerSelectors;ImportSpringfox.documentation.spi.DocumentationType;ImportSpringfox.documentation.spring.web.plugins.Docket;ImportSpringfox.documentation.swagger2.annotations.EnableSwagger2;/*** Swaggerconfig*/@Configuration @enableswagger2 Public classswagger2configuration {/** * * @return */@Bean PublicDocket Accesshello () {return NewDocket (documentationtype.swagger_2). GroupName ("Hello")//Defining Groups. Select ()//Selecting those paths and APIs will generate the document. APIs (Requesthandlerselectors.basepackage ("Com.springboot.controller"))//blocked Packet path. Paths (Regex ("/hello/.*"))//blocked interface Path. build ();//Create//. Apiinfo (Apiinfo ());//Configuration Instructions } /** * * @return */@Bean PublicDocket Accessuser () {return NewDocket (documentationtype.swagger_2). GroupName ("user")//Defining Groups. Select ()//Selecting those paths and APIs will generate the document. APIs (Requesthandlerselectors.basepackage ("Com.springboot.controller"))//blocked Packet path. Paths (Regex ("/user/.*"))//blocked interface Path. build ();//Create//. Apiinfo (Apiinfo ());//Configuration Instructions }}
3 Adding the test controller layer
PackageCom.springboot.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.PostMapping;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.springboot.model.User;ImportCom.springboot.util.ApiJSONUtil;ImportCom.springboot.util.JsonReturn;Importio.swagger.annotations.ApiOperation, @RestController @requestmapping ("/USER") Public classUsercontroller {Map<String,User> maps =NewHashmap<string,user>(); @ApiOperation (Value= "Add", notes= "User add action") @PostMapping ("/add") PublicString Add (@RequestBody user user)throwsexception{Maps.put (User.getuuid (), user); Jsonreturn Jsonreturn=NewJsonreturn (true); returnapijsonutil.objecttojsonstr (Jsonreturn); } @ApiOperation (Value= "Delete", notes= "User add action") @PostMapping ("/delete/{uuid}") PublicString Add (@PathVariable string uuid)throwsexception{maps.remove (UUID); Jsonreturn Jsonreturn=NewJsonreturn (true); returnapijsonutil.objecttojsonstr (Jsonreturn); } @ApiOperation (Value= "Modify", notes= "User add action") @PostMapping ("/update") PublicString Delete (@RequestBody user user)throwsexception{Maps.put (User.getuuid (), user); Jsonreturn Jsonreturn=NewJsonreturn (true); Jsonreturn.setobj (user); returnapijsonutil.objecttojsonstr (Jsonreturn); } @ApiOperation (Value= "Query", notes= "User add action") @PostMapping ("/query") PublicString query (@RequestBody map<string,string> Map)throwsexception{User User= Maps.get (Map.get ("UUID")); Jsonreturn Jsonreturn=NewJsonreturn (true); Jsonreturn.setobj (user); returnapijsonutil.objecttojsonstr (Jsonreturn); }}
4 Enter http://localhost:8080/swagger-ui.html#/View effect
Spring Boot integrated swagger UI (online automatic generation of RESTful interface documents + functional test function software, rapid development of front-end separation)