Swagger is an open source set of tools built on the OpenAPI specification to help us design, build, document, and use Rest apis. Swagger consists of three main parts: Swagger Editor, a browser-based Editor that we can use to write our OpenAPI specification. Swagger UI renders the OpenAPI specification we wrote as an interactive API document, and I’ll use a browser to view and manipulate our Rest API. Swagger Codegen it simplifies the build process by generating server stubs and client SDKS for any API defined by the OpenAPI (formerly known as Swagger) specification.
Add the dependent
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Copy the code
Configuration Swagger
Springfox provides a Docket object that allows us to flexibly configure Swagger properties. Create a New SwaggerConfiguration class and add the following:
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket createRestApi(a) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(
new ApiInfoBuilder()
.title("Rest API Documentation built on Swagger")
.description("For more please consult the service developer")
.contact(
new Contact("vincent"."https://blog.jiangyongkang.com"."[email protected]")
)
.termsOfServiceUrl("https://blog.jiangyongkang.com")
.version("1.0")
.build()
)
.select()
.apis(RequestHandlerSelectors.basePackage("spring.boot.swagger")) .paths(PathSelectors.any()) .build(); }}Copy the code
Adding a Test Interface
@API (tags = "Test interface ")
@RestController
@RequestMapping("/test")
public class TestController {
@PostMapping
@ ApiOperation (" POST request ")
publicResponseEntity<? > create(@ApiParam(name = "name", required = true, example = "vincent") @RequestParam("name") String name) {
return ResponseEntity.ok("success"); }}Copy the code
Actually just need to add the dependence, we restart the project, and then visit http://localhost:8080/swagger-ui.html in the browser can see the results