Attention:
If you are working on microservices, there is a need for mutual invocation between services, and the interfaces between services and APIs must generate the management documentation of the system. If you want to better manage your API, you want to have a tool to solve all the API-related things one-stop, then, swagger will be a good choice, the following for you to introduce swagger is the use of methods, if there is wrong, but also to correct!
1. Project Structure
Springboot-user-zuul-swagger-zuul Gateway Port 9501
ls-prevention-check-Service 1 Port 8091
microcloud-provider-company-Service 2 Port 8105
2. Implement the method of discharge
First, add dependent
added in three services, respectively swagger need to rely on two jar packages, add the following coordinates in the Pom.xml
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId> Springfox-swagger2</artifactid> 4 <version>2.7.0</version> 5 </ Dependency> 6 <dependency> 7 <groupid>io.springfox</groupid > 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version >2.7.0</version> </dependency>
Second, create the configuration class
Add swagger a configuration class in the service provider project to perform the basic configuration of swagger, respectively.
1 @Configuration2 @EnableSwagger23 Public classSwaggerconfig {4 5 @Bean6 PublicDocket Createrestapi () {7 return NewDocket (documentationtype.swagger_2)8 . Apiinfo (Apiinfo ())9 . Select ()Ten. APIs (Requesthandlerselectors.basepackage ("Com.zyc.controller")) One. APIs (Requesthandlerselectors.withclassannotation (API.class)) A. APIs (Requesthandlerselectors.withmethodannotation (apioperation.class)) - . Paths (Pathselectors.any ()) - . Build (); the } - - Privateapiinfo Apiinfo () { - return NewApiinfobuilder () +. Title ("Hello System API") -. Description ("Hello System interface Documentation description") +. Contact (NewContact ("Vker", "", "[email protected]")) A. Version ("1.0") at . Build (); - } - - @Bean - uiconfiguration Uiconfig () { - return NewUiconfiguration (NULL, "list", "Alpha", "schema", inUiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,false,true, 60000L); - } to}
Iii. Start-up class plus annotations
@EnableSwagger2
Iv. adding specific annotations to the classes or methods that need to be generated
1@Api ("Hello interface")2 @RestController3@RequestMapping ("/company")4 Public classHello {5 6@ApiOperation (value= "/get/hello method")7@GetMapping ("/get/hello")8 PublicString Hello () {9 return"Hello Zuul Springboot-microcloud-provider-company";Ten } One A}
Note: Here is just a little chestnut: the detailed annotated meanings are as follows:
1 @Api () for classes; A resource that identifies the class as Swagger2 tags– Express Description3 value– is also a description, you can use tags instead4 5 @ApiOperation () for methods; An operation that represents an HTTP request6 value is used for method description7 notes for prompting content8 9 Ten @ApiParam () for methods, parameters, field descriptions, add metadata for parameters (description or mandatory) One name– name of the parameter A value– Parameter Description - required– is required - the @ApiModel () is used for classes; Indicates a description of the class for which parameters are received by the entity class - value– indicates the object name - - @ApiModelProperty () for a method, a field, a description of the model property, or a change in the data operation + value– Field Description - name– overriding property names + datatype– overriding property types A required– is required at example– Illustrative Examples - hidden– Hidden - - @ApiImplicitParam () for methods - represents a separate request parameter - in @ApiImplicitParams () for methods that contain multiple @ApiImplicitParam - name– parameter Ming to value– Parameter Description + datatype– Data Types - paramtype– parameter Types the example– Illustrative Examples * $ @ApiIgnorePanax NotoginsengActing on the method, using this annotation swagger will ignore this interface
V. Focus: Add a document resource configuration class in the Springboot-user-zuul-swagger projectDocumentationConfig
Note: Springboot-user-zuul-swagger must integrate Zuul as a routing access
1 @Component2 @Primary3 Public classDocumentationconfigImplementsswaggerresourcesprovider{4 5 @Override6 PublicList<swaggerresource>get () {7List resources =NewArraylist<>();8Resources.add (Swaggerresource ("Hello Interface", "/study-proxy/company-proxy/v2/api-docs", "1.0"));9Resources.add (Swaggerresource ("Check System Interface", "/study-proxy/prevention-check/v2/api-docs", "1.0"));Ten returnresources; One } A - Privateswaggerresource Swaggerresource (string name, string location, string version) { -Swaggerresource Swaggerresource =NewSwaggerresource (); the swaggerresource.setname (name); - swaggerresource.setlocation (location); - swaggerresource.setswaggerversion (version); - returnSwaggerresource; + } -}
Vi. Add the relevant configuration in the Springboot-user-zuul-swagger application.yml:
1 Spring: 2 Application:3 name:springboot-user-zuul4Zuul:5 prefix:/study-proxy6 ignored-services: "*"7 8 Microcloud-provider-company:/company-proxy/**9 Ls-prevention-check:/ prevention-check/**
Vii. visit to Http://localhost:9501/swagger-ui.html
Using spring Cloud Zuul as a gateway to integrate multiple microservices into one swagger service under the MicroServices architecture