Spring Boot integrated Swagger UI (online automatic generation of RESTful interface documents + functional test function software, rapid development of front-end separation)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.