Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate swagger2 in SpringBoot Project

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to integrate swagger2 in the SpringBoot project", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to integrate swagger2 in the SpringBoot project.

Brief introduction

Swagger is a popular API development framework, which is based on the Open API statement (OpenAPI Specification,OAS), provides a solution for the entire API development cycle, and is a very large project (including design, coding, and testing, supporting almost all languages).

The general principle of springfox:

The general principle of springfox is that, in the process of initializing the spring context, the framework automatically loads some swagger-related bean into the current context according to the configuration, and automatically scans the system for those classes that may need to generate api documents, and generates corresponding information to cache. If the project MVC control layer uses springMvc, then all Controller classes are automatically scanned and corresponding document description data is generated. The data is in json format, which can be accessed through the path: project address / v2/api-docs, and then swaggerUI generates the corresponding document description interface based on the data. Because we can get this data, we can also generate our own pages.

SpringBoot integrates Swagger2

Introduce dependency io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0

Note: only swagger2 can be run above jdk1.8

Write a configuration class to configure Swagger@Configuration@EnableSwagger2public class SwaggerConfig {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("org.example.yourproject")) / / fill in the project package .configuration (PathSelectors.any ()) .build () } / / springfox provides us with a Docket (summary meaning) class that we need to inject into spring as a Bean. Obviously, we need a configuration file and tell the program in a way (obviously it will be an annotation) that this is a Swagger configuration file. Private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("using Swagger2 to build RESTful API in Spring Boot") .description ("rest api document building sharp weapon") .termsOfServiceUrl ("https://www.cnblogs.com/yrxing/") .contact (" xing ") .version (" 1.0") .build () }} / / springfox allows us to combine information into a class of ApiInfo, which is passed to DocketSwagger2 common annotations as construction parameters using @ Api (), @ ApiOperation () @ RestController@RequestMapping (value = "/ user", produces = APPLICATION_JSON_VALUE) / / configuration return value application/json@Api (tags = "user management") public class HelloController {ArrayList users = new ArrayList () @ ApiOperation (value = "get user list", notes = "get all user information") @ RequestMapping (value = {""}, method = RequestMethod.GET) public List hello () {users.add (new User ("logical", "luoji")); users.add (new User ("Ye Wenjie", "yewenjie"); return users } @ ApiModel (), @ ApiModelProperty () @ ApiModel (description = "user", value = "user") public class User {private String id; @ ApiModelProperty (value = "user") / / value attribute indicates the meaning of the field (description Description) private String username; @ ApiModelProperty (hidden = true) / / this annotation can be applied to a field or method, as long as the hidden property is true, the field or method will not be generated api document. Private String password; private String email; private Integer age; private Boolean enabled } @ ApiParam () @ ApiOperation (value = "get user details", notes = "get user details according to url's id") @ RequestMapping (value = "getUser/ {id}", method = RequestMethod.GET) public User getUser (@ ApiParam (naeme = "id", value = "user id", required = true) @ PathVariable (value = "id") String id) {return new User (id, "itguang", "123456") } / @ ApiParam, it should be noted that the parameters of this annotation method cannot be directly used on the method. @ ApiImplicitParams (), @ ApiImplicitparam () @ Api ("test case 1") @ Controller public class swaggerTestUse () {@ ApiOperation (value = "apiOperationSwaggerTest", notes = "apiOperationSwagger test") @ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value = "id input parameter", required = true, dataType = "Integer") ParamType = "query"), @ ApiImplicitParam (name = "brand", value = "brand", required = true, dataType = "BRAND", paramType = "body")}) public void apiOperationSwaggerTest (Integer id, Brand band) {}} I believe you have a deeper understanding of "how to integrate swagger2 in the SpringBoot project". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report