Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Swagger Technology in Java

Shulou Source: shulou.com Published: 2022-06-01 10:03:36 09月22日 Update

This article will explain in detail how to use Swagger technology in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The function and concept of Swagger

In the era of front and back end separation, we need to update interface information and test interface automatically in real time to realize front and back end separate development. As a result, swagger is generated.

Using swagger in a project

Here is an example of a 3.0.0 dependency

Io.springfox springfox-swagger2 3.0.0 io.springfox springfox-swagger-ui 3.0.0 io.springfox springfox-boot-starter 3.0.0 @ RestControllerpublic class HelloController {@ RequestMapping (value = "/ hello") public String hello () {return "hello" } @ Configuration@EnableSwagger2 / / Open swagger2public class SwaggerConfig {}

Then visit http://localhost:8080/swagger-ui/index.html

You can see the following interface for the swagger document

Configure swagger

Let's take a look at the underlying code and learn about it.

ApiInfo configuration @ Configuration@EnableSwagger2 / / enable swagger2public class SwaggerConfig {@ Bean public Docket docket () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ());} / configure swagger information apiInfo private ApiInfo apiInfo () {/ / author information Contact contact = new Contact ("Song Xianhui", "https://blog.csdn.net/sxh06"," xianhuisong@yeah.net ") Return new ApiInfo ("Song Xianhui's Api Documentation", "Learning swagger has no end", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0"," New ArrayList () }} swagger configuration scanning interface @ Configuration@EnableSwagger2 / / enable swagger2public class SwaggerConfig {/ / @ Bean// public Docket docket1 () {/ / return new Docket (DocumentationType.SWAGGER_2) .groupName ("grouping two"); / / @ Bean public Docket docket (Environment environment) {Profiles profiles=Profiles.of ("dev"); / / get the project environment boolean flag=environment.acceptsProfiles (profiles) Return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .groupName ("Song Xianhui") / / groupName ("Song Xianhui") / / enable (flag) / / enable configuration whether or not to start swagger flase cannot be accessed in the browser. Select () / RequestHandlerSelectors implements class configuration scanning mode / / basePackage specifies that the package / / any () to be scanned all / / none () does not scan / / withClassAnnotation () the annotation parameter on the scan class is an annotated reflection object / / withMethodAnnotation scan method. APIs (RequestHandlerSelectors.basePackage ("com. Sxh.swagger.controller ")) / / .apis (RequestHandlerSelectors.withMethodAnnotation (GetMapping.class)) / / filter what road strength filter request / / .filter (PathSelectors.ant (" / sxh/** ")) .build () } / / configure swagger information apiInfo private ApiInfo apiInfo () {/ / author information Contact contact = new Contact ("Song Xianhui", "https://blog.csdn.net/sxh06"," xianhuisong@yeah.net ") Return new ApiInfo ("Song Xianhui's Api Documentation", "Learning swagger has no end", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0"," New ArrayList () }}

If I only want to use swagger in the build environment, what if I don't use swagger in the formal environment? (enable=false | true)

Configure api document grouping

Multiple grouping

You can configure multiple Docket instances

@ Bean public Docket docket1 () {return new Docket (DocumentationType.SWAGGER_2) .groupName ("grouping one");} @ Bean public Docket docket2 () {return new Docket (DocumentationType.SWAGGER_2) .groupName ("grouping two");} `entity class configuration

This is the end of this article on "how to use Swagger technology in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Configuration grouping information interface annotation environment article technology author multiple documentation more project learning good practical code function content separation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info OPPO Reno Xiaomi NVidia