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 integrate swagger-ui2 with spring mvc

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What this article shares to you is about how spring mvc integrates swagger-ui2. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Spring mvc to build swagger-ui

1 add dependency

Com.google.guava guava 27.0.1-jre com.github.xiaoymin swagger-bootstrap-ui 1.9.6 io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0

2`add swagger resource file

3 interceptor releases swagger resource files

4. Swagger requests authorization

5 create a configuration class for swagger

Package swagger;import io.swagger.annotations.ApiOperation;import org.springframework.context.annotation.Bean;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;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 / / enable Swagger2@EnableSwagger2public class Swagger2Config extends WebMvcConfigurationSupport {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()). Select () / scan swagger comments in the specified package / / .apis (RequestHandlerSelectors.basePackage ("com.xia.controller")) / / scan all annotated api It is more flexible in this way. APIs (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class)). Build () } @ Bean private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("basic platform RESTful APIs") .description ("the RESTful-style interface document of the basic platform, with detailed content, greatly reduces the communication cost between the front and back ends, while ensuring that the code is highly consistent with the document, greatly reducing the time to maintain the document.") .termsOfServiceUrl ("http://xiachengwei5.coding.me") .version (" 1.0.0 ") .termsOfServiceUrl (" http://xxx.xxx.com") .license ("LICENSE") .licenseUrl ("http://xxx.xxx.com") .build ()) } @ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addRedirectViewController ("/ docApi/v2/api-docs", "/ v2/api-docs"); registry.addRedirectViewController ("/ docApi/swagger-resources/configuration/ui", "/ swagger-resources/configuration/ui"); registry.addRedirectViewController ("/ docApi/swagger-resources/configuration/security", "/ swagger-resources/configuration/security") Registry.addRedirectViewController ("/ docApi/swagger-resources", "/ swagger-resources");} @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ docApi/swagger-ui.html**") .addResourceLocations ("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler ("/ docApi/webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/");}}

6 load the swagger configuration class

7 access address of running project: ip/ project name / doc.html

Common notes on swagger:

The @ ApiIgnore annotation can ignore this class, method, parameter

@ Api (tags= {"comments on controller here"})

@ Api (tags= {"knowledge Base Interface"}) @ RestController@RequestMappingpublic class InfoController {

@ ApiOperation (value = "method description")

@ ApiOperation (value = "get detailed data of a law and regulation") @ RequestMapping (value = "/ info", method = RequestMethod.GET) public Info getInfoById (

@ ApiParam (name= "id", value = "method parameter description")

Public Info getInfoById (@ ApiParam (name= "id", value = "ID of laws and regulations") @ RequestParam ("id") String id)

@ ApiModelProperty (value= entity Field description)

@ ApiModelProperty (value= "ID") private String id

@ ApiModel (value = "entity class description")

@ ApiModel (value = "info: legal and regulatory entity") public class Info is how spring mvc integrates swagger-ui2. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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: 219

*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