MP Combat Series (10) SPRINGMVC Integrated Springfox+swagger2

Source: Internet
Author: User

This example is based on the previous combat series, and if the company framework uses JDK7 above and its spring+mybatis+springmvc/spring+mybatis PLUS+SPRINGMVC can refer directly to the instance.

However, it is advisable to use Jdk8+spring+mybatis PLUS+SPRINGMVC, as this example is based on this.

The reason for integrating Springfox+swagger2 is because the feature is integrated swagger because of the previous single SPRINGMVC.

This, I'll talk about it later.

First, import dependency

    <!--Swagger API Document management -        <Dependency>            <groupId>Com.mangofactory</groupId>            <Artifactid>Swagger-springmvc</Artifactid>            <version>1.0.2</version>        </Dependency>                <Dependency>            <groupId>Com.mangofactory</groupId>            <Artifactid>Swagger-models</Artifactid>            <version>1.0.2</version>        </Dependency>                <Dependency>            <groupId>Com.wordnik</groupId>            <Artifactid>Swagger-annotations</Artifactid>            <version>1.3.11</version>        </Dependency>                <Dependency>            <groupId>Com.google.guava</groupId>            <Artifactid>Guava</Artifactid>            <version>15.0</version>        </Dependency>                <!--Integrated Springfox -        <Dependency>           <groupId>Io.springfox</groupId>           <Artifactid>Springfox-swagger2</Artifactid>           <version>2.7.0</version>         </Dependency>                 <Dependency>           <groupId>Io.springfox</groupId>           <Artifactid>Springfox-swagger-ui</Artifactid>           <version>2.7.0</version>         </Dependency>

Ii. Preparation of Swaggerconfig.java

 PackageCom.swagger;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportSpringfox.documentation.builders.ApiInfoBuilder;Importspringfox.documentation.builders.RequestHandlerSelectors;ImportSpringfox.documentation.service.ApiInfo;ImportSpringfox.documentation.spi.DocumentationType;ImportSpringfox.documentation.spring.web.plugins.Docket;ImportSpringfox.documentation.swagger2.annotations.EnableSwagger2;/** *  * @authorYC*/@Configuration @enableswagger2 Public classswaggerconfig{@Bean PublicDocket API () {return NewDocket (documentationtype.swagger_2). Select (). APIs (Requesthandlerselectors.any ())    . Build (). Apiinfo (Apiinfo ()); }    Privateapiinfo Apiinfo () {return NewApiinfobuilder (). Title ("Interface API Management Documentation"). Description ("HTTP Open Interface"). Version ("1.0.0"). Termsofserviceurl ("Http://www.test.com"). License ("LICENSE"). Licenseurl ("Http://www.test.com"). build (); }}

Third, in the Spring-mvc.xml file to supplement the Bean

As the name implies, the Bean's role is to integrate swagger integrated spring

<class= "Com.mangofactory.swagger.configuration.SpringSwaggerConfig"/ >      

If you do not add the bean, you will see the following:

There is also the situation when the bean is configured to run the project:

Description: Resource is being acquired, but it will take a while to delay

Get a list of resources, load slowly, can't open, usually browser's sake, I use IE and Firefox can open to see the normal API interface documentation, but with Google Browser is always opened, this is not known for what reason, to be researched.

In addition to the issue of swagger version, if it is SPRINGMVC integration swagger, it is recommended to use more than 2, 3 or less, or it will appear as shown in the situation:

SPRINGMVC integration of the above issues in version 3 of swagger

Cause: The version is too low to be used

Solution: Get back to normal with a low version of swagger

Recommended use of the swagger version of 2.2.10

If it is springboot, use the version above Swagger3 directly. Springboot using the Swagger high version does not have this problem, I have tried it before.

MP Combat Series (10) SPRINGMVC Integrated Springfox+swagger2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.