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

Ultra-simple springboot+swagger2 for on-line interface debugging

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

Share

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

Foreword:

As a standard standard background development engineer, when the front-end interaction provides it with an interface, do you need to prepare interface documents for it? Do you have to pass the self-test before co-adjusting with him? Is it necessary to write interface calls (such as postman) before self-testing? As a responsible and progressive engineer, this must be done. You have to do everything, but you can do it in different ways, and the results and efficiency are also different, so let's share with you how the interfaces provided in the springboot project can easily and quickly provide a way of self-testing, or even testing directly to interface testers, that is, integrate into seagger2. Use the third-party plug-ins you still want to use.

Declare:

I do not like the article is very complex, the pursuit of simple and beautiful practicality, but in order to need partners can be directly in place, absolutely available, so directly paste the core code, it is convenient for you to copy it, so it adds an unfriendly complexity to the article.

Development environment:

1. Add dependencies

This defaults to the maven project we have used, otherwise please download the dependency-related java package by yourself. The project introduces the following dependencies:

Io.springfox

Springfox-swagger2

2.9.2

Io.springfox

Springfox-swagger-ui

2.9.2

2. Add configuration class

Package cn.seally.community.config

Import static com.google.common.base.Predicates.or

Import static springfox.documentation.builders.PathSelectors.regex

Import org.springframework.context.annotation.Bean

Import org.springframework.context.annotation.Configuration

Import com.google.common.base.Predicate

Import springfox.documentation.builders.ApiInfoBuilder

Import springfox.documentation.builders.RequestHandlerSelectors

Import springfox.documentation.service.Contact

Import springfox.documentation.spi.DocumentationType

Import springfox.documentation.spring.web.plugins.Docket

Import springfox.documentation.swagger2.annotations.EnableSwagger2

/ * *

* @ Description swagger online interface plug-in configuration class

* @ Date 31 August 2019

* @ author seallydeng

, /

@ Configuration

@ EnableSwagger2 / / Open online interface documentation

Public class SwaggerConfig {

/ * *

* @ Description

* @ Date 31 August 2019

* @ author seallydeng

* @ return

, /

@ Bean

Public Docket controllerApi () {

Return new Docket (DocumentationType.SWAGGER_2)

.apiInfo (new ApiInfoBuilder ()

.title ("title: Seally Web Interface document") / / title

.description ("the system interface mainly includes xxx, yyy and other functional modules") / / description

Contact (new Contact ("dningcheng", "http://www.xxx.com/web"," dningcheng@163.com "))

.version ("1.0.0") / / version number

.build ()

.select ()

.apis (RequestHandlerSelectors.basePackage ("cn.seally.community.controller")) / / configure the scanned controller class package

.upload (paths ()) / / configure path interfaces that conform to the specified rules to generate online interfaces, which are used to customize which interfaces are displayed in ui.

.build ()

}

@ SuppressWarnings ("unchecked")

Private Predicate paths () {

Return or (regex ("/ user/api/.*?"))

}

}

3. The controller adds interface description information by annotations.

Package cn.seally.community.controller

Import org.springframework.web.bind.annotation.DeleteMapping

Import org.springframework.web.bind.annotation.GetMapping

Import org.springframework.web.bind.annotation.PathVariable

Import org.springframework.web.bind.annotation.PostMapping

Import org.springframework.web.bind.annotation.PutMapping

Import org.springframework.web.bind.annotation.RequestBody

Import org.springframework.web.bind.annotation.RequestMapping

Import org.springframework.web.bind.annotation.RestController

Import cn.seally.community.common.ApiResponse

Import cn.seally.community.model.base.SystemUser

Import io.swagger.annotations.Api

Import io.swagger.annotations.ApiOperation

/ * *

* @ Description controller used to demonstrate swagger online interface documentation

* @ Date 31 August 2019

* @ author seallydeng

, /

@ Api (tags= {"system Home Hotmap related Interface"}) / / marked on the class: used to note the interface group name

@ RestController

@ RequestMapping ("/ demo")

Public class DemoController {

@ ApiOperation (value= "get user information", notes= "username is the only attribute, and a single user can obtain it directly through username") / / marked in the method: used to note the description of the API.

@ GetMapping (value= "/ user/query")

Public ApiResponse getUserInfo (String username) {

Return ApiResponse.success ("get information through")

}

@ PostMapping (value= "/ user/save", produces= "application/json")

Public ApiResponse saveUserInfo (@ RequestBody SystemUser user) {

Return ApiResponse.success ("user information saved successfully")

}

@ PutMapping (value= "/ user/update", produces= "application/json")

Public ApiResponse updateUserInfo (@ RequestBody SystemUser user) {

Return ApiResponse.success ("user information modified successfully")

}

@ DeleteMapping (value= "/ user/delete/ {username}", produces= "application/json")

Public ApiResponse deleteUserInfo (@ PathVariable ("username") String username) {

Return ApiResponse.success ("user information deleted successfully")

}

}

4. Start the project and access it

Enter the access path in the browser to access it. Usually, if the project does not have a configuration path, it can be accessed directly: http://ip:port/swagger-ui.html. If springboot has configured the project path, such as:

Then the path name of the access is: http://ip:port/seally-community-web/swagger-ui.html

If the project is normal, you can see the following interface:

Let's click on a way to take a look:

The interface is too large, so only part of it is intercepted. Here, we only need to enter the request parameters, click execute, and we can see the Olympic execution results, such as:

There is also a download button on the right that can be downloaded directly to a return value in json format.

Usually, various methods generate parameters for us in the swagger API. You only need to enter a value to execute the request directly. The following is an example of a post request initiated by application/json:

Summary:

Is swagger2 easy to use? the above only demonstrates the annotations that you think are useful. Of course, it also has a series of annotations to help us explain the interfaces and input parameters in detail, such as:

@ ApiImplicitParams: used on the requested method to represent a set of parameter descriptions

@ ApiImplicitParam: used in the @ ApiImplicitParams annotation to represent a specific parameter and for each information description of a single parameter

@ ApiResponses: used on the request method to represent a set of responses

@ ApiResponse: used in @ ApiResponses to indicate a specific parameter of the response and to explain all aspects of the specific parameter

@ ApiModel: used on the response class to represent a message that returns response data

@ ApiModelProperty: used in conjunction with @ ApiModel to describe the attribute information of the response class on the properties of the response class

These annotations can be used as needed, usually as long as the parameters are well defined and have specific semantics, I don't think it is necessary to write such detailed notes.

How, is it very simple? friends in need, please try it as soon as possible. If you find it useful, you might as well give me a like and encourage me to never stop on the way to sharing.

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