Swagger and SpringMVC Project Integration, swaggerspringmvc

Source: Internet
Author: User

Swagger and SpringMVC Project Integration, swaggerspringmvc
Integrate Swagger and SpringMVC Projects

To facilitate the management of API interfaces in projects, I found a lot of information on API interface management on the Internet. I feel that Swagger is the most popular at present. It has powerful functions and beautiful UI interfaces, it also supports online testing, so I carefully studied the use of Swagger. The following describes how to integrate Swagger with my SpringMVC project:

Final API management interface:

Detailed steps:

Step 1: Introduce relevant jar packages to 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>
Step 2: Add a custom config File
Package com. spg. apidoc. common. configer; import org. springframework. beans. factory. annotation. autowired; import org. springframework. context. annotation. bean; import org. springframework. context. annotation. configuration; import com. mangofactory. swagger. configuration. springSwaggerConfig; import com. mangofactory. swagger. models. dto. apiInfo; import com. mangofactory. swagger. plugin. enableSwagger; import com. mangofac Invalid. swagger. plugin. swaggerSpringMvcPlugin;/*** Project name: apidoc ** @ description: * @ author Wind-spg * @ create_time: february 10, 2015 10:27:51 * @ version V1.0.0 **/@ Configuration @ EnableSwagger // Loads the spring beans required by the frameworkpublic class MySwaggerConfig {private SpringSwaggerConfig springSwaggerConfig; /*** Required to autowire SpringSwaggerConfig */@ Autowired public void setSpringSwa GgerConfig (SpringSwaggerConfig springSwaggerConfig) {this. springSwaggerConfig = springSwaggerConfig;}/*** Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc * framework-allowing for multiple swagger groups I. e. same code base * multiple swagger resource listings. */@ Bean public SwaggerSpringMvcPlugin customImplementation () {return new SwaggerSpringMvcPlugin (this. springSwagge RConfig). apiInfo (). includePatterns (".*? ");} Private ApiInfo apiInfo () {ApiInfo apiInfo = new ApiInfo (" 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"); return apiInfo ;}}
Step 3: add the configuration to the Spring container as follows:
<bean class="com.spg.apidoc.common.configer.MySwaggerConfig" />
Step 4: Add related APIAnnotation to the Code as follows:
@ ResponseBody @ RequestMapping (value = "addUser", method = RequestMethod. POST, produces = "application/json; charset = UTF-8") @ ApiOperation (value = "Add User", httpMethod = "POST", response = BaseResultVo. class, notes = "add user") public String addUser (@ ApiParam (required = true, name = "postData", value = "json data of user information ") @ RequestParam (value = "postData") String postData, HttpServletRequest request) {LO GGER. 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); int result = userService. addUser (user); return buildSuccessResultInfo (result );}

Note:
@ ApiOperation and @ ApiParam are added API annotations. The parameters are described as follows:
@ ApiOperation (value = "interface description", httpMethod = "Interface request method", response = "interface return parameter type", notes = "interface release description "; for other parameters, refer to the source code;
@ ApiParam (required = "required or not", name = "parameter name", value = "parameter description"

Step 5: add the Swagger UI Configuration

Download the SwaggerUI project on GitHub and copy all the content under dist to the webapp of the local project. The result directory is shown in:

Step 6: Modify index.html

Add the token in index.html

So far, all configurations are complete. Start your project and visit http: // localhost: 8080/{projectName}/index.html to see the following page:

The final demo of the project can be found at GitHub.
Https://github.com/itboyspg/spg-code/tree/master/apidoc
Refer:
Https://github.com/martypitt/swagger-springmvc
Https://github.com/swagger-api/swagger-ui

Related Article

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.