Configuring the Swagger plugin in SPRINGMVC

Source: Internet
Author: User
Tags unique id

First, Introduction

The goal of Swagger is to define a language-independent standard interface for the rest API that allows users to discover and understand the capabilities of a computer service without having to access the source code. When properly defined through swagger, the user can understand and interact with the remote service with the least amount of implementation logic. Interfaces that are similar to low-level programming.

Second, implement step 1, add Maven dependency
<Dependency>    <groupId>Io.springfox</groupId>    <Artifactid>Springfox-swagger2</Artifactid>    <version>2.6.1</version></Dependency>
2. Swagger Configuration Class
@Configuration @enableswagger2//@ComponentScan (basepackageclasses = jgbjbaseinfocompanyapi.class) or@ComponentScan (basepackages = "Com.summersoft.ts.schedule.supervision.controller")//the path of the package to scan Public classSwaggerconfig {@Bean PublicDocket Swaggerspringmvcplugin () {return NewDocket (documentationtype.swagger_2). Apiinfo (Apiinfo ()). Select ()//Select which paths and APIs will generate document. APIs (Requesthandlerselectors.any ())//monitoring of all APIs. Paths (Pathselectors.any ())//Scan all Paths. Build (); }    /*** API Specific Information * *@return     */    Privateapiinfo Apiinfo () {apiinfo apiinfo=NewApiinfo ("Docking Service Platform API documentation",//title"",//Description"1.0",//version"",                "",                "",//Signature""//Signature Link        ); returnApiinfo; }}
3. Swagger annotations

Swagger will scan the class file with Swagger annotations under the package path configured in Swaggerconfig and finally generate a sequence of scanned JSON files ...

Swagger Note Description:https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel

@Api : Used on the class, explain the role of the class, it should be noted that the older version of the value of the name of the scan generated, after 1.5 to use tag to represent the class name
@Api (tag= "Usercontroller", description = "User-related Api")
@ApiOperation : Use the method to illustrate the role of the method
@ApiOperation (value = "Find User", notes = "Find User", HttpMethod = "GET", produces = Mediatype.application_json_utf8_value)

@ApiParam : Used in the parameter list to indicate the meaning of the parameter

@ApiParam (value = "Create or update distance current (month)") Integer time

@ApiImplicitParams : Use the method to include a set of parameter descriptions
@ApiImplicitParam : Use the @apiimplicitparams annotation to specify all aspects of a request parameter
   paramtype: Where to put the parameter
   header–> Acquisition of request parameters: @RequestHeader
   query–> request parameters: @RequestParam
   path (for restful interfaces) –> Request parameter acquisition: @PathVariable
   body (infrequently used)
   form (infrequently used)
   name: Parameter name
   datatype: Parameter type
   required: Whether the parameter must be passed
   value: The meaning of the parameter
    DefaultValue: The default value of the parameter
        @ApiImplicitParams ({
Span style= "FONT-SIZE:12PX;" >        @ApiImplicitParam (name = "id", value = "Unique id", required = true, DataType = "Long", Paramtype = "Path"),
       })

@ApiResponses : Used to represent a set of responses
@ApiResponse : Used in @apiresponses, generally used to express a wrong response information
Code: number, e.g. 400
Message: Information, such as "request parameters not filled out."
Response: class that throws an exception
@ApiResponses (value = {
@ApiResponse (code = +, message = "No Name provided")
})

@ApiModel : Describes the information of a model (this is typically used when post creation, using a scenario such as @requestbody, where request parameters cannot be described using @apiimplicitparam annotations)
@ApiModel (value = "User entity class")
@ApiModelProperty : Describe the properties of a model
@ApiModelProperty (value = "Logged in user")

Third, Swagger-ui

With the configuration information above, Swagger will help us scan all of the class information and generate a JSON file. To get the JSON file-friendly display in front of people, you need to use the Swagger-ui component:

1, swagger-ui use instructions :https://swagger.io/docs/swagger-tools/

2, download Swagger-ui , in the WebApp directory to create a new swagger directory, the Dist directory of files, put into the swagger directory, and modify the index.html file, the default is to get the JSON of the API from the connection Http://petstore.swagger.io/v2/swagger.json, where you need to modify the URL value to http://{ip}:{port}/{ The Projectname}/api-docs form, the value in {} is filled in according to its own circumstances. For example, my URL value is: Http://localhost:8080/vouchers/api-docs. In addition, you need to configure Spring MVC's resource release:<mvc:resources mapping= "/swagger/**" location= "/swagger/"/>

Tips: The default dist directory does not have so many files, Swagger-ui can be customized configuration, this is used in our project, do not change the project name, the project name Dynamic acquisition:https://files.cnblogs.com/files/jmcui/ Swagger.zip

3,swagger-ui How to display the interface sort:

apissorter : Apply sort to API/tag list. It can be ' alpha ' (sort by name) or function (see Array.prototype.sort () for how the sort function works). The default is that the server returns the same order.

Operationssorter : Applies a sort to the list of actions for each API. It can be ' alpha ' (sorted alphabetically), ' method ' (sorted by HTTP method) or function (see Array.prototype.sort () to know how the sort function works). The default is that the server returns the same order.

Configuring the Swagger plugin in SPRINGMVC

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.