Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code for Spring boot to configure swagger

Shulou Source: shulou.com Published: 2022-06-03 10:26:42 10月11日 Update

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.

Tags: Documentation interface configuration code system title question front end method more version help development test solution difference easy tedious inevitable simple and easy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia MariaDB Linux Docker