Preface
In the previous blog, we registered the service to Eureka. We can see from Eureka's UI interface which services have been registered on Eureka Server. However, if we want to see which RESTful interface methods are provided by the current service, we have no way to obtain them. The traditional method is to sort out a service interface document for communication between developers. In this case, many times, it will cause inconsistencies between the document and the code, such as the code being changed, but the interface document has not been changed, and Swagger2 provides us with a perfect solution. Let's take a look at how Swagger2 solves the problem.
1. Introduce the jar package that Swagger2 depends on
<!-- 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>
2. Initialize the configuration of Swagger2
@Configuration @EnableSwagger2 // Enable Swagger2 public class Swagger2 { @Bean public Docket createRestApi() {// Create API basic information return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.chhliu.jpa")))// Scan all APIs under this package that need to be displayed in Swagger, except for @ApiIgnore annotation annotated.paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() {// Basic information about creating APIs, which will be displayed in the Swagger UI. Return new ApiInfoBuilder() .title("Build RESTful APIs in Spring Boot using Swagger2")// 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. When the project starts, the configuration class is initialized.
3. Improve API document information
public interface SonarControllerI { @ApiOperation(value="get Url information corresponding to Sonar of the project group", notes="get Url information corresponding to Sonar of the project group based on 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 method parameter information. It should be noted here that the paramType parameter needs to be configured as path, otherwise an error will be reported when accessing the interface method in the UI @GetMapping("/get/{id}") SonarUrl get(@PathVariable Long id); @ApiOperation(value="get all Url information corresponding to the project group Sonar") @GetMapping("/get/all") List<SonarUrl> getAll(); } Note: paramType represents the type of the parameter, optional values are "path", "body", "query", "header", "form"
4. Improve the return 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, hidden", required=true, dataType="Long")// Use this annotation to describe the property information. When hidden=true, this property will not display in the api @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ApiModelProperty(value="URL link address") @Column(name="URL") private String url; @ApiModelProperty(value="Project Group") @Column(name="TEAM") private String team; @ApiModelProperty(value="Department") @Column(name="DEPARTMENT") private String department; ...omit getter, setter method... }5. Start the application
1. Enter in the browser: http://localhost:7622/swagger-ui.html
2. The results are as follows:
6. API document access and testing
In addition to providing API interface viewing function, 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 to everyone's learning and I hope everyone will support Wulin.com more.