In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares to you is about the whole process of generating interface documents for Springboot integration swagger2. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Introduction of Swagger
Swagger is a specification and complete framework for generating, describing, invoking, and visualizing RESTful-style web services. The goal is to enable the client and the file system to update files at the same speed as the server, parameters and models are tightly integrated into the server. To put it simply, swagger is an intermediate software that can generate interface development documents based on restful style and support testing.
Second, the advantages of using swagger
1. For back-end developers
There is no need for handwritten Wiki interface to spell a large number of parameters, avoiding handwriting errors is less intrusive to the code, using full annotation, the development of a simple method of parameter name modification, addition, and reduction of parameters can take effect directly, without manual maintenance disadvantages: increased development costs, write the interface also have to write a set of parameter configuration
2. For front-end development
The backend only needs to define the interface and automatically generate documents. It is easy to adjust the interface functions and parameters at a glance. If something goes wrong, test the interface directly and check the parameters and return values in real time. You can quickly locate whether it is a front-end or back-end problem.
3. For testing
But for testing the function without front-end interface UI, you can directly use it to test the interface. It is easy to operate and can be operated without knowing the specific code.
Third, springboot integration swagger use
1. Create a new maven project (structure is as follows:)
2. Configure the pom.xml file
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.dds.sbswagger sb-swagger 0.0.1-SNAPSHOT sb-swagger Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 org.projectlombok lombok 1.18.6 org.springframework.boot spring-boot-maven-plugin
3. Program startup class
Package com.dds.sbswagger;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @ author dds * / @ SpringBootApplication@Slf4jpublic class SbSwaggerApplication {public static void main (String [] args) {SpringApplication.run (SbSwaggerApplication.class, args) Log.info ("\ n\ t" + "Application demo is running! Access URLs:\ n\ t "+" swagger-ui:\ t http://127.0.0.1:8080/swagger-ui.html\n\t" + "- -");}}
4. SwaggerConfig configuration class
Package com.dds.sbswagger.config;import io.swagger.annotations.ApiOperation;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;import java.util.Collections / * * @ author DDS * @ date 13:55 on 2019-9-10 * / @ Configuration@EnableSwagger2public class SwaggerConfig {@ Bean public Docket api () {return new Docket (DocumentationType.SWAGGER_2) .select () .apis (RequestHandlerSelectors.basePackage ("com.dds.sbswagger.controller")) / / classes annotated with ApiOperation will generate the interface document .apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class)) .build () .apiInfo (apiInfo ()) } private ApiInfo apiInfo () {return new ApiInfo ("Spring Boot project integration Swagger instance document", "my official account of Wechat: Brother Avenue Qi, welcome to follow us." , "API V1.0", "Terms of service", new Contact ("https://www.cnblogs.com/jstarseven/"," jstarseven@163.com ")," Apache "," http://www.apache.org/", Collections.emptyList ();}} "
5. Entity class model
Package com.dds.sbswagger.model;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;/** * @ author DDS * @ date on 2019-9-10 13:55 * / @ ApiModel ("user entity") @ Datapublic class User {/ * user Id * / @ ApiModelProperty ("user id") private int id / * * user name * / @ ApiModelProperty (value = "user name", example = "zhangdan", required = true) private String name; / * * user address * / @ ApiModelProperty (value = "user address", example = "Haidian District of Beijing", required = true) private String address; / * * user phone number * / @ ApiModelProperty (value = "user phone number", example = "15689652367", required = true) private String phone / * * user age * / @ ApiModelProperty (value = "user age", example = "24", required = true) private Integer age;}
6. Interface development
Package com.dds.sbswagger.controller;import com.dds.sbswagger.model.User;import io.swagger.annotations.*;import org.springframework.web.bind.annotation.* / * @ author DDS * @ date, 2019-9-10 13:55 * / @ RestController@RequestMapping ("/ user") @ Api (tags = "user-related interface", description = "provide user-related Rest API") public class UserController {@ PostMapping ("/ add") @ ApiOperation (value = "New user interface", notes = "Mobile number and password are required, age is required. But it must be a number) @ ApiImplicitParams ({@ ApiImplicitParam (name = "name", value = "user name", required = true, paramType = "form"), @ ApiImplicitParam (name = "address", value = "user address", required = true, paramType = "form"), @ ApiImplicitParam (name = "phone", value = "user mobile number", required = true, paramType = "form"), @ ApiImplicitParam (name = "age", value = "user age", required = true) ParamType = "form", dataType = "Integer")}) public boolean addUser (@ RequestBody User user) {return false } @ ApiOperation ("find the user interface through id") @ GetMapping ("/ find/ {id}") public User findById (@ PathVariable ("id") int id) {return new User () } @ ApiOperation ("update user information interface") @ PutMapping ("/ update") @ ApiResponses ({@ ApiResponse (code = 400, message = "request parameters are not entered"), @ ApiResponse (code = 404, message = "request path is not correct"), @ ApiResponse (code = 405, message = "unknown error")}) public boolean update (@ RequestBody User user) {return true } @ ApiOperation ("remove user interface") @ DeleteMapping ("/ delete/ {id}") public boolean delete (@ PathVariable ("id") int id) {return true;}}
7. Swagger interface display
The above is the whole process of Springboot integrating swagger2 to generate interface documents. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.