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 write the code for Spring boot to configure swagger

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Spring boot configuration swagger code how to write, for this problem, this article describes in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Why use Swagger

In the actual development, as the back-end, we always provide interfaces to the front-end or other systems. Every time we write the code, we inevitably need to write the interface documents. First, it is a tedious task to write the interface documents. Secondly, from the interface to the interface documents need fields, even typesetting and so on. In addition, if we provide interfaces for multiple systems, we may need to write documents according to the requirements of different systems, so is there a way for us to provide good interface documents to the front end in the development phase? even we can expose the generated interface documents for other systems to call, so I only need a copy of the code.

Spring boot configuration swagger

1. Import maven dependencies

Io.springfox springfox-swagger2 2.6.1 io.springfox springfox-swagger-ui 2.6.1 com.github.xiaoymin swagger-bootstrap-ui 1.9.6

2.swagger configuration class

The switch of @ EnableSwagger2 / / Swagger indicates that Swagger@Configuration / / declares the package @ Value ("${swagger.title}") private String title of the current configuration class public class SwaggerConfiguration {@ Value ("${swagger.title}") private String basePackage; / / controller interface. / / title of the current document @ Value ("${swagger.description}") private String description; / / detailed description of the current document @ Value ("${swagger.version}") private String version / / version of the current document @ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage (basePackage)) .build (PathSelectors.any ()) } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title (title) .description (description) .version (version) .build ();}}

3.application.yml

# configuration swaggerswagger: basePackage: com.xx.demo.controller # package name title: title # title description: project document # description version: V1.0 # version number

4. Used in controller

@ Api (tags = {"test class"}) @ RestController@RequestMapping ("/ test") public class TestController {@ ApiOperation (value = "test method") @ GetMapping ("/ xrx") public String xrx () {return "hello";}}

5. Visit swagger

Http://localhost:8080/swagger-ui.html

Http://localhost:8080/doc.html

This is the answer to the question about how to write the code for Spring boot to configure swagger. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

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