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

Example Analysis of Spring Boot Integration Swagger Test api Construction

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

Share

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

Editor to share with you the Spring Boot integration of Swagger test api build 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 know it!

What is Swagger?

What is Swagger: THE WORLD'S MOST POPULAR API TOOLING

According to the official website:

Swagger Inspector: a development tool for testing API and generating OpenAPI. Swagger Inspector was created to address the three main goals of developers.

Perform a simple API test

Generate OpenAPI documents

Explore new API features

My understanding of Swagger is a specification and complete framework for generating, describing, invoking, and visualizing RESTful-style Web services. To put it simply, Swagger is a powerful interface management tool and provides front-and back-end separation solutions for multiple programming languages. According to my use, of course I am only the easiest to use, I feel that Swagger has the following advantages:

Swagger can be integrated into the code to automatically generate API documents by annotating and writing comments during development.

Separate the front end and background, there will be no over-reliance.

The interface is clear, whether it is the real-time display of editor or the display of ui are very user-friendly, if you only use markdown to write, but also have to struggle with how to show, it is very painful.

Build a project

Step1. Import dependency

Io.springfox springfox-swagger2 2.6.1 io.springfox springfox-swagger-ui 2.6.1 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test

Step2. Write swagger configuration classes

In order to use swagger function, you must provide configuration class, mainly configure ui interface information, and configure scan location. Swagger will scan all services according to the configured path to generate api.

The core RequestHandlerSelectors.basePackage ("com.simple.spring.boot.controller") is here to configure the location of the scan package we need.

@ Configuration@EnableSwagger2public class SwaggerConfig {@ Beanpublic Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.simple.spring.boot.controller")) .build (PathSelectors.any ()) } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("Swagger2 is used in Spring Boot to build RESTful APIs") .description ("myapp") .termsOfServiceUrl ("http://blog.csdn.net/SimpleWu") .version (" 1.0") .title ();}}

Step3. Write springboot startup classes

@ ComponentScan (basePackages= {"com.simple.spring.boot.controller"}) also needs to configure the scan path.

SpringBootApplication@ComponentScan (basePackages= {"com.simple.spring.boot.controller"}) public class SwaggerApplication {public static void main (String [] args) {SpringApplication.run (SwaggerApplication.class, args);}}

Step4. Create a front-end controller

@ RestController@Api (tags = "swgger testing service", description = "swgger testing service") @ RequestMapping (value= "/ simple/wu") public class TestController {@ ApiOperation (value= "testing POST method", notes= "testing POST method") @ ApiImplicitParam (name = "token", value= "ID", required = true, dataType = "token") @ RequestMapping (value= "hello", method=RequestMethod.POST) public String post (@ RequestBody String token) {books.put (book.getId (), book) Return "success";}}

@ Api (tags = "swgger testing service", description = "swgger testing service") specifies the name of the service provided by a class

@ ApiOperation (value= "test POST method", notes= "test POST method") specifies the name of a request

@ ApiImplicitParam (name = "token", value = "token", required = true, dataType = "String") specifies that the parameter corresponding to the name is the token, and the corresponding parameter field token,required = true indicates that this parameter is required, and dataType represents the data type.

Step5. Start the service

From the above code, we specify the request as POST. On the UI interface, we will see a large class with the service name of swgger Test Service. After clicking in, you can see the request contained in it. If you specify the type of request, then you cannot do unit testing. After specifying, we will see a request named Test POST method and you need to enter the required parameter token to complete our unit test.

We can run the main method directly through the SwaggerApplication class to perform the service, and the port number defaults to 8080.

Swagger address: http://localhost:8080/swagger-ui.html can be accessed only by adding swagger-ui.html after the address.

We can visit this location to see the UI interface, the interface is simple and easy to use, I will not take screenshots here.

Step. Summary

Swagger official document: https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

One of the biggest advantages of swagger is the ability to synchronize api and documents in real time.

In the process of project development, it happened many times: the code was modified but the document was not updated, the front end was developed according to the old document, and the problem was only found in the process of joint debugging (of course, according to the opening and closing principle, the modification of the interface is not allowed, but in the unstable phase of the project, this situation is difficult to avoid).

The above is all the content of the article "sample Analysis of Spring Boot integrating Swagger Test api Construction". 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!

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