Swagger and SPRINGMVC Project integration
In order to facilitate the management of the Project API interface, in the Internet to find a lot of information on API interface management, feel the most popular is swagger, powerful, UI interface beautiful, and support online testing, etc., so I carefully studied the use of the next swagger, The following is a detailed description of how to integrate swagger with individual SPRINGMVC projects:
STEP1: The relevant jar package is introduced into the project:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <version.spring>3.2.9.RELEASE</version.spring> <Version.jackson>2.4.4</Version.jackson> </Properties> <dependencies>....<dependency> <groupId>Com.mangofactory</groupId> <artifactid>Swagger-springmvc</artifactid> <version>0.9.5</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-annotations</artifactid> <version>${version.jackson}</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-databind</artifactid> <version>${version.jackson}</version> </Dependency> <dependency> <groupId>Com.fasterxml.jackson.core</groupId> <artifactid>Jackson-core</artifactid> <version>${version.jackson}</version> </Dependency> </dependencies>
STEP2: Adding a custom config file
PackageCom.spg.apidoc.common.configer;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.Configuration;ImportCom.mangofactory.swagger.configuration.SpringSwaggerConfig;ImportCom.mangofactory.swagger.models.dto.ApiInfo;ImportCom.mangofactory.swagger.plugin.EnableSwagger;ImportCom.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;/** * Project Name: Apidoc * * @description: * @author WIND-SPG * @create_time:2015 year February 10 morning 10:27:5 1 * @version V1.0.0 * */@Configuration@EnableSwagger//Loads the spring beans required by the framework Public class myswaggerconfig{ PrivateSpringswaggerconfig Springswaggerconfig;/** * Required to Autowire springswaggerconfig * * @Autowired Public void Setspringswaggerconfig(Springswaggerconfig springswaggerconfig) { This. springswaggerconfig = Springswaggerconfig; }/** * Every Swaggerspringmvcplugin bean is picked up by the SWAGGER-MVC * framework-allowing for multiple SW Agger groups i.e. same Code base * Multiple Swagger resource listings. */ @Bean PublicSwaggerspringmvcplugincustomimplementation() {return NewSwaggerspringmvcplugin ( This. Springswaggerconfig). Apiinfo (Apiinfo ()). IncludePatterns (".*?"); }PrivateApiinfoApiinfo() {Apiinfo Apiinfo =NewApiinfo ("My Apps API Title","My Apps API Description","My Apps API Terms of service","My Apps API contact Email","My Apps API Licence Type","My Apps API License URL");returnApiinfo; }}
STEP3: Add this configuration to the spring container as follows:
<bean class="com.spg.apidoc.common.configer.MySwaggerConfig" />
STEP4: Add the relevant apiannotation in your code, as follows:
@ResponseBody @RequestMapping(Value ="AddUser", method = Requestmethod.post, produces ="Application/json; Charset=utf-8 ")@ApiOperation(Value ="Add Users", HttpMethod ="POST", response = baseresultvo.class, notes ="Add User") PublicStringAddUser(@Apiparam(Required =true, name ="PostData", value ="User information JSON data")@RequestParam(Value ="PostData"String PostData, HttpServletRequest request) {Logger.debug (String.Format ("at function,%s", PostData));if(NULL= = PostData | | Postdata.isempty ()) {return Super. Buildfailedresultinfo (-1,"Post data is empty!"); } UserInfo user = Json.parseobject (postdata, Userinfo.class);intresult = Userservice.adduser (user);returnBuildsuccessresultinfo (result); }
Description
Where @apioperation and @apiparam are added API-related annotations, the parameters are described below:
@ApiOperation (value = "Interface description", HttpMethod = "Interface Request Mode", Response = "interface return parameter type", notes = "interface Release notes"; Other parameters can refer to source code;
@ApiParam (required = "parameter Required", name = "parameter name", value = "Parameter specific description"
STEP5: Add Swagger UI Configuration
Download the Swaggerui project on GitHub and copy all the content under Dist to the local project WebApp below, as shown in the results directory:
STEP6: Modify Index.html
Modify Http://petstore.swagger.wordnik.com/v2/swagger.json in index.html to Http://localhost:8080/{projectname}/api-docs
So far, all configuration is complete, start your project, visit http://localhost:8080/{projectname}/index.html to see the following page:
Project final demo visible personal GitHub
Https://github.com/itboyspg/spg-code/tree/master/apidoc
Reference:
Https://github.com/martypitt/swagger-springmvc
Https://github.com/swagger-api/swagger-ui
Swagger and SPRINGMVC Project integration