Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of SpringBoot Integration Swagger2

Shulou Source: shulou.com Published: 2022-06-03 10:08:02 10月19日 Update

Editor to share with you SpringBoot integration of Swagger2 example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

Preface

As the preferred framework for micro-services, springBoot provides a large number of interface services for other services. The interface peer needs the most recent interface documentation in real time.

Swagger can automatically generate online documentation for web projects through code and comments, using swagger here.

When SpringMVC in SpringBoot code uses the automated configuration class WebMvcAutoConfiguration, the way to integrate Swagger2 is as follows.

If WebMvcConfigurationSupport; is used in the configuration process of SpringMVC, the following integration approach is not appropriate.

1. Spring Boot Web integrates Swagger2 process

There are two main processes for Spring Boot Web project integration Swagger2:

Add Swagger2 related dependencies.

Configure the Swagger2 configuration class.

1.1.Adding Swagger2 related dependencies

First, add Swagger2-related dependencies to the Spring Boot Web project:

Org.springframework.boot spring-boot-starter-web io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 1.2, configuration Swagger2 configuration class

@ Configuration@EnableSwagger2public class Swagger {/ / create Docket's Bean @ Bean public Docket docket () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) / / select () function returns an ApiSelectorBuilder instance that controls which interfaces are exposed to Swagger to display .select () / / packages to be scanned .apis (RequestHandlerSelectors.basePackage (" Com.example.controller ") / / Select the API path. Build (PathSelectors.any ()). } / / create the basic information of the document public ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("title of Swagger UI") .description ("write the interface in restful style") .termsOfServiceUrl (") .version (" 1.0 ") .build ();}} II. Common notes for configuring Swagger2 interfaces.

2.1, @ Api request class description

Written above the controller class definition to illustrate the role of the class.

@ Api (value = "Swagger Test Control", description = "Control class that demonstrates the use of Swagger", tags = "Swagger Test Control Tag") 2.2, description of @ ApiOperation method

Write above the REST interface to illustrate the function of the method.

@ ApiOperation (value= "create user", notes= "create user from User object") 2.3, @ ApiImplicitParams and @ ApiImplicitParam method parameter description @ ApiImplicitParams: used on the requested method Contains a set of parameter instructions @ ApiImplicitParam: description of a single parameter name: parameter name value: description of the parameter in Chinese characters, Explain whether the required: parameter must pass paramType: where to put the parameter header-- > request parameter acquisition: @ RequestHeader query-- > request parameter acquisition: @ RequestParam path (for restful API)-> request parameter acquisition: @ PathVariable body (request body)-- > @ RequestBody User user Form (normal form submission) dataType: parameter type Default String Other values dataType= "int" defaultValue: default values for parameters-@ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value = "ID", dataType= "Long"), @ ApiImplicitParam (name = "user") Value = "user", dataType = "User")}) 2.4, @ ApiResponses, and @ ApiResponse method return value @ ApiResponses: method return object description @ ApiResponse: description of each parameter code: number For example, 400 message: information For example, "request parameters are not filled" response: class that throws an exception-@ ApiResponses ({@ ApiResponse (code = 400,message = "insufficient permissions"), @ ApiResponse (code = 500) Message = "server internal exception")}) 2.5, @ ApiModel and @ ApiModelProperty

@ ApiModel is used above JavaBean to represent a JavaBean. This is commonly used when creating post, using scenarios such as @ RequestBody, and when request parameters cannot be described using the @ ApiImplicitParam annotation.

@ ApiModelProperty describes a field of the object when it receives parameters with the object.

@ ApiModel (description = "student") public class Student {@ ApiModelProperty (value = "primary key id") private String id; @ ApiModelProperty (value = "name", required = true) private String name; @ ApiModelProperty (value = "age", required = true) private int age;} 2.6.Other notes

ApiIgnore: use this annotation to ignore the API and not document the interface.

@ ApiError: the information returned when an error occurs

The above is all the contents of the article "sample Analysis of SpringBoot Integration Swagger2". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Parameters interfaces methods configuration integration objects documents comments services information users articles processes projects examples analysis code functions content time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux macOS NVidia Shulou Tech Info