请求地址:http://localhost:8080/server.context-path/swagger-ui.html
maven依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
swagger配置类
import org.springframework.beans.factory.annotation.Value; 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; /** * 配置Swagger,用于测试rest,默认只在本地环境开启 * */ @EnableSwagger2 @Configuration public class Swagger2Config { /** * 是否开启swagger */ @Value(value = "${swagger.enabled}") Boolean swaggerEnabled; /** * 扫描对应的包路径,生成API * * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) // 是否开启 .enable(swaggerEnabled).select() // 要扫描的包 .apis(RequestHandlerSelectors.basePackage("cn.com.test.channel.controller")) .paths(PathSelectors.any()).build().pathMapping("/"); } /** * 设置API 信息 * * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("xxx") .description("xxx") .version("1.0.0") .build(); } }
常用注解:
- Api
- ApiModel
- ApiModelProperty
- ApiOperation
- ApiParam
- ApiResponse
- ApiResponses
- ResponseHeader
Ex:
@Data
@ApiModel
public class UserRepay {
@NotNull(message = "用户ID不能为空")
@ApiModelProperty("用户ID")
private Long userId;
}
@RestController
@Api(tags = "还款管理", description = "还款管理API")
@RequestMapping("repay")
public class UserRepayController extends BaseController {
@Resource
private UserRepayService userRepayService;
@ApiOperation(value = "获取还款列表", notes = "获取还款列表 userId用户ID必填")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Long", paramType = "query"),
@ApiImplicitParam(name = "pageNumber", value = "页码", dataType = "Long", paramType = "query")
})
@RequestMapping(value = "/get", method = RequestMethod.POST)
public Object get(Long pageNumber, Long pageSize,
@RequestBody @ApiParam(value = "还款") UserRepay userRepay) {
return ResponseUtil.getSuccessMap(userRepayService.get(pageNumber, pageSize, userRepay));
}
}