In most cases, companies will require detailed interface documentation. Documentation is sometimes a headache in the case of rapid development. And swagger's automatic document generation function can help us reduce the workload. Document modification can also be modified at any time in the code, for items. An urgent project is also a good solution.
Relevant dependencies need to be introduced for spring boot projects
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.8.1</version> </dependency>
Relevant dependencies provide us with a visual document interface and support for document export.
We also need to add related configurations. Sprboot generally recommends using annotated configurations instead of xml, so we add config configuration classes here.
@Configuration public class SwaggerConfig implements WebMvcConfigurer { public void setSwagger_is_enable(String swagger_is_enable){ enable=true; //The switch displayed here for the control document home page can be dynamically controlled by configuring the incoming control } @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(enable) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.jzfq.swagger")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboot utilize swagger structure api File") .description("Simple and elegant restfun Style, http://blog.csdn.net/saytime") .version("1.0") .build(); }
In this configuration class, we add some home page information of ui interface. In Bean configuration, we need to add the scanning path of document annotations so that our annotations can be scanned and parsed to
Open swagger's automatic configuration on spring boot's startup class using annotation @EnableSwagger2
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @SpringBootApplication public class Swagger01Application { public static void main(String[] args) { SpringApplication.run(Swagger01Application.class, args); } }
So spring boot integrates swagger 2. Next we can generate a demo test interface document.
Our parameters may be received directly using vo. We can create a User entity class.
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; @ApiModel(description = "User Entity Class") public class User implements Serializable { @ApiModelProperty(value ="Full name",allowableValues = "Zhang San,Li Si,Wang Wu") private String name; @ApiModelProperty(value ="Gender",allowableValues = "male,female,Unknown") private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
Next, create a controller layer to invoke through post man
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SwaggerTestController { @ApiOperation(value = "test generation Swagger", notes = "test generation Swagger", tags = "swagger Modular") @ApiImplicitParams( @ApiImplicitParam(name = "age", value = "Age", paramType = "body", dataType = "Integer", required = true) ) @ApiResponses( @ApiResponse(code = 400, message = "Request parameters not filled in") ) @PostMapping("/test") public void test( User user){ System.out.println("Spring boot Integrate Swagger2"); } }
Finally, by visiting the address of http://localhost:8080/doc.html, we can see such an interface, which integrates a simple swagger. There are many annotations for swagger. Interested users can search for various uses of annotations to meet our daily development.