springboot2.0整合swagger
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.1.1</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.1.1</version> 10 </dependency>
配置:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("DataUtils-API文档") .description("工具项目") .termsOfServiceUrl("www.baidu.com") .version("1.0") .build(); } }
使用:
@RestController @EnableSwagger2 @Api("用户信息") public class UserInfoApi { @PostMapping("/getInfo") @ApiOperation(value = "查询用户信息") @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "姓名",required = true,dataType = "String")}) public UserEntity getInfo(@RequestBody UserEntity user){ System.out.println(user.toString()); return user; }
//如果不用@requestbody注解会拿不到参数,不知道啥原因;用了该注解,在swaggerUi页面请求又会报错;暂时还未找到原因 @PostMapping("/setInfo") @ApiOperation(value = "设置用户信息",code = 200,response = UserEntity.class) @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "姓名",required = true,dataType = "String")}) public UserEntity setInfo(@RequestBody UserEntity user){ System.out.println(user.toString()); return user; } }
以上内容原创来自每特学院,每特学员学习并整理!
给学院打波广告:
http://www.mayikt.com/
浙公网安备 33010602011771号