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

Spring 4.2.2 and above and swagger integration solutions and trampled pits

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Because the version of spring used by the company is too high, there will be some problems when integrating swagger, and most of the instances on the Internet are of relatively low version. In order to make friends step in less, I will record the process of integration here:

1. Introduce the relevant jar packages (springfox-swagger2, springfox-swagger-ui) of spring and swagger, and configure them in pom.xml:

Io.springfox springfox-swagger2 2.4.0 org.springframework spring-core org.springframework spring-beans Org.springframework spring-context org.springframework spring-context-support org.springframework Spring-aop org.springframework spring-tx org.springframework spring-orm org .springframework spring-jdbc org.springframework spring-web org.springframework spring-webmvc Org.springframework spring-oxm io.springfox springfox-swagger-ui 2.4.0

Reminder: note that springfox-swagger2 has introduced jar related to spring when it is integrated, especially the versions of spring-context and spring-context-support are completely inconsistent with the versions used in the project. There are many problems of package conflicts when the project is started. The relevant jar packages of spring are filtered out when the pom.xml file is introduced.

two。 Write the configuration class for Swagger:

Package com.ml.honghu.swagger.web; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType Import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2 @ EnableWebMvc @ EnableSwagger2 @ Configuration @ ComponentScan (basePackages = {"com.ml.honghu.**.rest"}) public class SwaggerConfig {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.ml.honghu")) Build (PathSelectors.any ()) .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("Interface list v1.0") .description ("Interface Information") .termsOfServiceUrl ("http://honghu.com") .contact (new Contact (",") "HongHu") .version ("1.1.0") .build () }}

3. Configure the filter in the spring-mvc.xml file to filter out the relevant access configuration of swagger:

4. Service configuration item

@ Api ("Regional Services") @ RestController @ RequestMapping (value = "/ rest/area") public class AreaService {@ Autowired private AreaService areaService @ ApiOperation (value = "region list", httpMethod = "GET", notes = "region list") @ IsLogin @ ResponseBody @ RequestMapping (value = "treeData", method = RequestMethod.GET) public List treeData (@ ApiParam (required= true, value = "region ID") @ RequestParam (required=false) String extId, HttpServletResponse response) {List mapList = Lists.newArrayList (); List list = areaService.findAll () For (int item0; I

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report