How to construct API documents based on Swagger2 in SpringBoot
In this article Xiaobian for you to introduce in detail "SpringBoot based on Swagger2 how to build API documents", the content is detailed, the steps are clear, the details are handled properly, I hope this "SpringBoot based on Swagger2 how to build API documents" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge bar.
Add dependencies
Io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
Second, create a Swagger2 configuration class
Package com.offcn.config;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;@Configuration// indicates that this class is a configuration class, which is equivalent to the xml configuration file @ EnableSwagger2 / / in spring to open the online document public class SwaggerConfig {/ / 1. Declare the properties of the api document private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("Swagger2 is used in Spring Boot to build RESTful APIs") .description ("excellent employment") .termsOfServiceUrl ("http://www.ujiuye.com/") .contact") .version ("1.0") .title () } / / configure core configuration information public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.offcn.controller")) .pat hs (PathSelectors.any ()) .build ();}}
Third, modify Controller to add document notes
Add notes to API through the @ ApiOperation annotation
Add a description to the parameter through the @ ApiImplicitParams@ApiImplicitParam annotation
Package com.offcn.controller;import com.offcn.dao.UserDao;import com.offcn.entity.User;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController @ RequestMapping ("/ rest") @ RestControllerpublic class RestFulController {@ Autowired private UserDao userDao; @ GetMapping ("/ getUserById") @ ApiOperation (value= "find specified id user information", notes= "find user information based on id") @ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value= "user ID", required = true, dataType = "Integer"),}) public User getUserById (Integer id) {User user = userDao.getOne (id); return user } @ DeleteMapping ("/ del") @ ApiOperation (value= "delete specified id user information", notes= "delete user information based on id") @ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value= "user ID", required = true, dataType = "Integer"),}) public String delUserById (Integer id) {userDao.deleteById (id); return "success";}}
4. View Swagger2 documents
Restart the project
Visit:
Http://localhost:8080/swagger-ui.html
Read this, the "SpringBoot based on Swagger2 how to build API documents" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.