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

SpringBoot Learning (3)-- springboot Rapid Integration of swagger documents

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

Share

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

@[toc]

Introduction Advantages

The backend automatically generates beautiful and standardized interface documents according to swagger syntax.

Do interactive testing.

disadvantage

Intrusive, affecting program execution, especially when passing parameters.

note

Swagger is divided into version 1.2 and version 2.0, which are quite different. Swagger1.2 is swagger-ui ; swagger2.0 is springfox-swagger. The usage described in this article is a newer version, springfox-swagger.

Release production, close swagger to prevent leakage of project interface documentation, be ***

Introduction of swagger component

pom.xml

io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 Code Combat

I see a lot of bloggers say swagger configuration code should be in the same level directory as the project startup file, i.e. as follows

However, moved into the config directory, after testing, is also normal, then it depends on personal habits.

DemoApplication.java

package com.example; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;//Let Spring load this configuration with the @Configuration annotation.// Then enable Swagger2 with the@EnableSwagger2 annotation.@ Configuration@EnableSwagger2public class DemoSwagger { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //Specify the path of packets to scan .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("project api documentation") .description("swagger access tutorial") .version("1.0") .build(); }}

Because spring security has been configured before, after entering http://localhost:8080/swagger-ui.html in the browser URL, it will be blocked. After entering the previously configured user password, the effect is as follows:

Because we tested user login and user permissions before, there are already some interface methods in the controller, but let it default like this, obviously the user experience is not good, so we continue to add swagger comments in the previous userController.

@Api: Used on a class to describe what the class does.@ ApiOperation: Describes the function of the method.

For more detailed comments, see the official documentation for common comments.

UserController.java

package com.example.controller; import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.stereotype.Controller;import org. springframework.web.bind.annotation.RequestMapping;import org. springframework. web. bind. annotation.RequestMethod;import org. springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("user")@Api(value = "user module description," description = "add, delete, change, check")public class UserController { @RequestMapping(value = "/addUser", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "add user", notes = "put some information for test judgment") String addUser() { return "This is an added user!!! "; } @RequestMapping(value = "/deleteUser", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "delete user", notes = "put some information for test judgment") String deleteUser() { return "This is a deleted user!!! "; } @RequestMapping("/updateUser") @ResponseBody @ApiOperation(value = "modify user", notes = "put some information for test judgment") String updateUser() { return "This is a modified user!!! "; } @RequestMapping(value = "/findAllUsers", method = RequestMethod.PUT) @ResponseBody @ApiOperation(value = "query user", notes = "put some information for test judgment") String findAllUsers() { return "This is a query user!!! "; }}

The renderings are as follows

Open a specific item as follows

Obviously, with Chinese annotations, the document is more readable.

To be clear, when writing @RequestMapping annotations, I usually use shorthand, such as the modify user method in the demo above. But swagger is intrusive, if you do not specify RequestMethod type, it will list a lot of them, such as GET, HEAD, POST, PUT, OPTIONS, PATCH, and other specified good, it is one.

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