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 use Swagger Technology in Java

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

Share

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

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.

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