In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how springboot integrates swagger. I hope you will get something after reading this article. Let's discuss it together.
Brief introduction of springboot Integration swagger
Swagger provides the most powerful and easy-to-use tools to take full advantage of the OpenAPI specification.
Official website: https://swagger.io/
Preparatory work
Introduction of pom.xml jar: 2.9.2
Io.springfox springfox-swagger2 ${swagger.version} io.springfox springfox-swagger-ui ${swagger.version} purpose
In the past, using swagger is to write the parameters introduced by api directly on classes, methods, and entities, which gives people a feeling that the code is not very clean, so using the mode of yaml file editing makes the code look easier.
Project structure
1. Create a SwaggerConfig class
Package com.honghh.bootfirst.config;import io.swagger.annotations.ApiOperation;import org.springframework.context.annotation.Bean;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 / * ClassName: SwaggerConfig * Description: * * @ author honghh * @ date, 2019-02-20 14:28 * / @ Configuration@EnableSwagger2public class SwaggerConfig {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () / classes annotated with ApiOperation Generate the classes under the .apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class)) / / package, and generate the interface documentation / / .apis (RequestHandlerSelectors.basePackage ("com.honghh.bootfirst.controller")) .build (PathSelectors.any ()). } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("boot-demo") .description ("boot-demo document") .termsOfServiceUrl ("http://www.boot-demo.cn") .version (" 1.0.0 ") .build ();}}
two。 Introduce swagger display layer code. I will upload the code to Ciyun https://gitee.com/honghh/boot-demo.git.
3. Configure the yaml file
# required fields! Swagger specification version, must be filled in 2. 0, otherwise the YAML will not be used for other components of Swagger swagger:'2. 0 required # required fields! The description of the metadata info: # interface document describing the API interface information, the description: swagger description document, makes everything so simple. # version number version: 1.0.0 # Interface title title: boot-demo#Swagger provides the test case, host specifies the hostname of the test, if it is not specified, it is the current host, you can specify the api prefix defined by host: localhost:8080#, must have / start, and the host of the test case is: host+bashPath#basePath: / boot-demo# specifies the protocol to call the interface Must be: "http", "https", "ws", "wss". The default is http.- to indicate that it is an array element, that is, schemes accepts an array parameter schemes:-http-https# corresponds to the Accept of the http protocol header request. The caller can accept the type. The default is * / *. The defined type must be the Mime Types,RestfulAPI defined by the http protocol, which is generally defined as application/json#. These two are global settings for all interfaces. In the refined interface, you can also correspond to these two attributes to override the global attribute produces:-application/json# definition interface data paths: / myInfo: # necessary fields! To define the operation method of HTTP, it must be the method defined by http protocol get: tags:-MyInfo user information # interface summary summary: user information # interface description description: query all the information of all users, user name Alias parameters:-name: id description: user ID in: query type: integer required: true # returns value description Necessary automatic responses: # return http status code 200: description: all user information or user collection information # description return value schema: # return value format Array,integer,string is optional. Boolean $ref:'# / definitions/myInfo'# defines the data model definitions: r: type: object properties: code: description: status code 0: success non-0: failure type: integer format: int32 msg: description: failure reason type: string myInfo: type: object properties: id: description: ID Type: integer format: int32 age: description: age type: integer name: description: name type: string
4. To start the project, enter: http://localhost:8080/swagger/index.html runs as shown in the figure
After reading this article, I believe you have a certain understanding of "how springboot integrates swagger". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.