1, the introduction of

The separation of front and back ends has gradually become a standard development method for Internet projects. The front-end and back-end are developed by different personnel.

However, the communication cost in project development also increases, which mainly lies in the communication between front-end developers and back-end developers on WebAPI interface. Swagger2 can solve the problem well, it can dynamically generate Api interface documents, reduce the communication cost and promote efficient project development.

2. Add dependencies

<! --swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> < version > 2.8.0 < / version > < / dependency > < the dependency > < groupId >. IO springfox < / groupId > < artifactId > springfox swagger - UI < / artifactId > < version > 2.8.0 < / version > < / dependency > <! <dependency> <groupId>com.github. Xiaoymin </groupId> <artifactId>swagger-bootstrap- UI </artifactId> The < version > 1.9.3 < / version > < / dependency >Copy the code

Add swagger2 configuration class

@Configuration @EnableSwagger2 public class Swagger2Config { @Value("${sw2.swTitle}") private String swTitle; @Value("${sw2.swTermsOfServiceUrl}") private String swTermsOfServiceUrl; @Value("${sw2.swVersion}") private String swVersion; @Value("${sw2.swDescription}") private String swDescription; */ @bean public Docket createRestApi() {return new? / @bean public Docket createRestApi() {return new Docket (DocumentationType SWAGGER_2). ApiInfo (apiInfo ()) / / call apiInfo method, create a apiInfo instance, it is to show the document page information content. The select () Under the exposed out of the path / / instance / / if you don't want to expose an interface, you can use the following comments / / @ ApiIgnore in this way, the interface is not exposed to the swagger2 page / /. Apis (RequestHandlerSelectors. Any ()) / / scan all apis (RequestHandlerSelectors withMethodAnnotation (ApiOperation. Class)) / / scan contains "ApiOperation" annotation methods .paths(PathSelectors.any()) .build(); Private ApiInfo ApiInfo () {return new ApiInfoBuilder() // page title.title (swTitle) // Clause address .termSofServiceurL (swTermsOfServiceUrl).version(swVersion) // description.description (swDescription).build(); }}Copy the code

application.yml

The information is written in the configuration file, which gives you more flexibility

Sw2: swTitle: Spring Boot Swagger2 Build RESTful API swTermsOfServiceUrl: swVersion: 1.0 swDescription: DESCRIBES the API interfaceCopy the code

4. Test interface

@apiOperation (value = "query books by ID ",notes =" restfull style ",httpMethod = "GET") @APIIMPLICITParam (name = "id",value = "Primary key ",dataType="int",required = true) @requestMapping (value="/list2/{id}") @responseBody public Book list2(@PathVariable(value = "id") Integer id){ Book book = bookService.findById(id); return book; }Copy the code
  • @Api: Decorates the entire class to describe what Controller does
  • ApiOperation: Describes a method, or interface, of a class
  • @APIParAM: Single parameter description
  • @APIModel: Accept arguments with objects
  • ApiProperty: A field describing an object when receiving a parameter with the object
  • @apiResponse: HTTP responds to one of the descriptions
  • @apiresponses: Overall description of HTTP responses
  • @apiIgnore: Ignore the API with this annotation
  • @apiError: Information returned when an error occurs
  • ApiImplicitParam: Describes a request parameter. You can configure the Chinese meaning of the parameter and set the default value for the parameter
  • @APIIMPLICITParams: Describes a list of request parameters annotated by multiple @APIIMPLicitParam parameters

5, access,

Start the project

swagger-ui: http://IP:PORT/swagger-ui.html
Copy the code

Enhanced UI: http://IP:PORT/doc.htmlCopy the code

Feel or enhanced version of the use of a bit more suitable for actual use