Automatically generate documents using swagger

Source: Internet
Author: User
Tags web services
what is Swagger.

Swagger is a normative and complete framework for generating, describing, invoking, and visualizing RESTful Web services.

Springfox, formerly known as SWAGGER-SPRINGMVC, is an open source API Doc framework that can be used to document the methods of our Controller, based on swagger.

Official website: http://swagger.io/

Open Source Address: http://springfox.github.io/springfox/


Swagger use the first step: import dependencies

<!--Swagger--
<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>

Step Two: Open @EnableSwagger2 annotations
@SpringBootApplication
@RestController
@EnableSwagger2 public
class DemoApplication {


Step three: Visit http://localhost:8080/swagger-ui.html




How to intercept the fixed Package,url API if all controller APIs are intercepted

Fourth step: Customizing the URL API
All interfaces can be intercepted at once, or block-by-block
@Configuration public class Swagger2configuration {/* @Bean public Docket Accesstoken () {return new docket (documen tationtype.swagger_2). GroupName ("order")//define group. Select ()//Select those paths and APIs to generate document. APIs (requesthandlerselectors.ba  Sepackage ("Com.zto.springboot.controller"))//Blocked packet path. Paths (Pathselectors.regex ("/web/.*")//Blocked interface path. Build ()// Create. Apiinfo (Apiinfo ());  Configuration Instructions}*/@Bean public Docket AccessToken2 () {return new docket (documentationtype.swagger_2). GroupName ("all Interfaces")// Define groups. Select ()//Select those paths and APIs to generate document. APIs (Requesthandlerselectors.basepackage ("Com.zto.security.web.control Ler ")//intercept the packet path//.paths (Pathselectors.regex ("/user/.* "))//intercept the interface path. Paths (Pathselectors.any ())//block all interfaces. Bu ILD ()//Create. Apiinfo (Apiinfo ()); Configuration description} private Apiinfo Apiinfo () {return new Apiinfobuilder (). Title ("XXX Project Interface Document")//title. Description ("XXX Item Item Description XXX Project description XXX project description XXX project description "//description. Contact (" xxx "," http://www.baidU.com "," xxxxxx@qq.com "))//Contact//.termsofserviceurl (" http://www.roncoo.com ")//The URL of the terms of service//. License (" Apache Licens E Version 2.0 ")//Open Source Agreement//. Licenseurl (" Https://github.com/springfox/springfox/blob/master/LICENSE ")//Address. Version ("
	1.0 ")//version. Build (); }

}


Fifth step: Control layer Annotation Usage instructions
@Api: Modifies the entire class, describing the role of the controller
@ApiOperation: A method that describes a class, or an interface
@ApiParam: A single parameter description
@ApiModel: Using an object to receive parameters
@ApiProperty: When a parameter is received with an object, a field of the object is described
@ApiResponse: HTTP response 1 Description
@ApiResponses: HTTP response Overall Description
@ Apiignore: Use this note to ignore the API
@ApiError: The information returned by the error
@ApiParamImplicitL: A request parameter
@ApiParamsImplicit multiple request parameters




@ApiOperation
@ApiOperation (value = "New User Service", notes = "New user")
@PostMapping Public user
Create (@Valid @RequestBody user User, Bindingresult errors) {
    return user;
}
@ApiModelProperty
public class Userquerycondition {
	
	private String username;

	@ApiModelProperty (value = "User Age start Value")
	private int ages;

	@ApiModelProperty (value = "User Age End Value")
	private int ageto;
	
	Private String xxx;


@ApiParam
@ApiOperation (value = "User Delete Service", notes = "Delete User")
@DeleteMapping ("/{id:\\d+}") Public
Void Delete (@ApiParam (" User ID ") @PathVariable String ID) {
   System.out.println (ID);
}


Final effect


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.