Spring cloud integrates Swagger2 to build the APIs and swagger2restful services
Preface
In the previous blog, we registered the service to Eureka. From the Eureka UI, we can see which services have been registered to the Eureka Server. However, if we want to see which RESTful interface methods are provided by the current service, we will not be able to obtain them. The traditional method is to sort out a service interface document for developers to communicate with each other, in this case, the document and code may be inconsistent, for example, the Code has been changed, but the interface document has not been changed, swagger2 provides us with a perfect solution. Next, let's take a look at how Swagger2 solves the problem.
1. Introduce the jar package on which Swagger2 depends
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
Ii. initialize the Swagger2 Configuration
@ Configuration @ EnableSwagger2 // enable Swagger2 public class Swagger2 {@ Bean public Docket createRestApi () {// create API basic information return new Docket (DocumentationType. SWAGGER_2 ). apiInfo ()). select (). apis (RequestHandlerSelectors. basePackage ("com. chhliu. jpa ") // scan all the APIs to be displayed in Swagger under the package, except for those marked by @ ApiIgnore annotations. paths (PathSelectors. any ()). build ();} private ApiInfo apiInfo () {// basic information for creating an API. The returned new ApiInfoBuilder () is displayed in the Swagger UI (). title ("using Swagger2 in Spring Boot to build RESTful APIs") // API title. description ("RESTful APIs provided by rdcloud-jpa") // API description. contact ("chhliu @") // contact. version ("1.0") // version number. build ();}}
Note: This configuration class needs to be created in the Application directory at the same level. This configuration class is initialized when the project is started.
3. Complete API Documentation Information
Public interface SonarControllerI {@ ApiOperation (value = "Get the Url Information of the project team Sonar", notes = "Get the Url Information of the project team Sonar Based on the id ") // use this annotation to describe interface method information @ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value = "SonarUrl table ID", required = true, dataType = "Long ", paramType = "path")}) // use this annotation to describe the method parameter information. Note that the paramType parameter must be set to path. Otherwise, when accessing the interface method in the UI, @ GetMapping ("/get/{id}") SonarUrl get (@ PathVariable Long id); @ ApiOperation (value = "get all Url information corresponding to Sonar of the project team ") @ GetMapping ("/get/all") List <SonarUrl> getAll ();}
Note: paramType indicates the parameter type. The optional values are "path", "body", "query", "header", and "form"
Iv. Improve the returned type information
@ Entity (name = "SONAR_URL") public class SonarUrl implements Serializable {/*****/private static final long serialVersionUID = 1L; @ ApiModelProperty (value = "primary key ", hidden = false, notes = "primary key, hide", required = true, dataType = "Long") // use this annotation to describe the property information. When hidden = true, this property does not display @ Id @ GeneratedValue (strategy = GenerationType. AUTO) private Long id; @ ApiModelProperty (value = "URL link address") @ Column (name = "URL") private Str Ing url; @ ApiModelProperty (value = "Project TEAM") @ Column (name = "team") private String TEAM; @ ApiModelProperty (value = "Department ") @ Column (name = "DEPARTMENT") private String department ;...... Omit the getter and setter methods ...... }
5. Start the application
1. Enter: http: // localhost: 7622/swagger-ui.html in the browser
2. The result is as follows:
Vi. API document access and Testing
In addition to API interface viewing, Swagger also provides debugging and testing functions.
The test results are as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.