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 configure Spring Boot+Swagger_UI

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

Share

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

This article mainly explains "Spring Boot+Swagger_UI how to configure," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Spring Boot+Swagger_UI how to configure"!

One: pom.xml dependency

io.springfox springfox-swagger2 2.8.0 io.springfox springfox-swagger-ui 2.8.0

2: application.yaml switch configuration

# swaggerswagger: switch: true

Three: SwaggerConfig.java configuration

@Configuration@EnableSwagger2public class SwaggerConfig { @Value("${swagger.switch}") private boolean swaggerSwitch; @Bean public Docket createRestApi() { Docket docket = new Docket(DocumentationType.SWAGGER_2); if (swaggerSwitch) { docket.enable(true); } else { docket.enable(false); } docket.apiInfo(apiInfo()).select() //Only classes annotated with ApiOperation can generate interface documents .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //only the classes under the package will generate the interface document .paths(PathSelectors.any()).build().securitySchemes(security()); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Demo").description("Interface Document").termsOfServiceUrl("").version("0.1").build(); } private List security() { return newArrayList(new ApiKey("token", "token", "header")); }}

IV: Example of interface configuration code

/** * @Title: list * @Description: Test * @return void return type * @throws */ @ApiOperation(value="Message List Query",notes="Message List Query") @ApiImplicitParams({@ApiImplicitParam(name="userId",value="userID",required=true,dataType="int")}) @ApiResponses(value= {@ApiResponse(code = 200,message="Sucess",response=MsgPushInfoEntity.class,responseContainer="List")}) @RequestMapping(value="/list",method=RequestMethod.POST) public List list(@RequestBody MsgPushInfoEntity msg) { logger.info("===========" + msgPushInfoService.list().size() + "==========="); logger.error("===========" + msgPushInfoService.list().size() + "==========="); return new ArrayList(); }

Five: swagger-ui display

VI: Swagger Notes

1.@ Api

This annotation labels a Controller (Class) as a swagger resource (API). By default, Swagger-Core scans only for parsing files with

@Api annotation class, and automatically ignores annotations for other class resources (JAX-RS endpoints, Servlets, etc.). This annotation contains several important attributes:

tags

API grouping labels. APIs with the same tag will be grouped together and displayed in a group.

value

If tags are not defined, value will be used as tags for Api

description

Detailed description of API, no longer used after version 1.5.X, but actually found to be still available in version 2.0.0

2.@ ApiOperation

Describes an operation or HTTP method on a specified path. Different actions with the same path are grouped into the same operand.

Different HTTP request methods and path combinations form a unique operation. The attributes of this annotation are:

value

A simple description of the operation, 120 letters in length, 60 Chinese characters.

notes

A detailed description of the operation.

httpMethod

The action name of the HTTP request. Optional values are: "GET", "HEAD", "POST", "PUT", "Delete", "OPTIONS" and "PATCH".

code

The default is 200, valid values must conform to standard HTTP Status Code Definitions.

3.@ ApiImplicitParams

The container class for AppiImplicitParam, stored as an array.

@ApiImplicitParam

Annotate a single parameter of the API. Although the @ApiParam annotation is bound to JAX-RS parameters, this @ApiImplicitParam annotation can be used in a uniform manner.

Defining parameter lists is also the only way to define parameters in Servelet and non-JAX-RS environments. Note that the annotation @ApiImplicitParam must be

Included in annotation @ApiImplicitParams. The following important parameter properties can be set:

name

parameter name

value

Short description of parameter

required

Required parameter

dataType

Parameter type, which can be class name or basic type (String, int, boolean, etc.)

paramType

The incoming (request) type of the parameter. Optional values are path, query, body, header or form.

3.@ ApiParam

Added meta information description for parameters. This annotation can only be used in JAX-RS 1.x/2.x integrated environments. Its main attributes are:

required

Required parameter

value

Brief description of parameters

4.@ ApiResponses

Wrapper class for annotation @ApiResponse, array structure. Even if you need to use an @ApiResponse annotation, you need to include the @ApiResponse annotation in

Comment @ApiResponses.

5.@ ApiResponse

Describes the possible return results of an operation. This annotation can be used to describe all possible success and error codes when a REST API request occurs. It can be used or not.

Use this annotation to describe the return type of the operation, but the return type of a successful operation must be defined in @ApiOperation. If the API has different return types, you need to define the return value separately and associate the return types. Swagger, however, does not support annotations with the same return code and multiple return types. Note: This comment must be included in the @ApiResponses comment.

code

HTTP request return code. Valid values must conform to standard HTTP Status Code Definitions.

message

Text messages that are easier to understand

response

Returns type information, must use fully qualified class names, such as "com.xyz.cc.Person.class".

responseContainer

If the return type is a container type, you can set the corresponding value. Valid values are "List", "Set" or "Map", any other invalid values are ignored.

Comments on Model

For Model annotations, Swagger provides two: @ApiModel and @ApiModelProperty, which describe the Model and attributes within the Model, respectively.

6.@ ApiModel

Provides a description of additional information about the Swagger model. Within the operation annotated @ApiOperation, all classes will be automatically introspected,

However, this annotation can be used to make some more detailed model structure descriptions. The main attributes are:

value

Alias of model, default is class name

description

Detailed description of the model

7.@ ApiModelProperty

Comments on the model attribute, the main attribute values are:

value

Short description of properties

example

Sample values for the property

required

Is it a required value?

7: Questions about TOKER

For security reasons, each request to the API requires authentication and authorization of the user. At present, the mainstream authentication method uses the request header to pass the token, that is, the user obtains a token after logging in, and then uses this token to request the API every time. If you want to use swagger-UI for API testing, you must explicitly specify the token parameter for each API that needs to be validated.

At this point, I believe that everyone has a deeper understanding of "Spring Boot+Swagger_UI how to configure", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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

Internet Technology

Wechat

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

12
Report