swagger的使用
所需依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
加入配置:
@EnableSwagger2
@ComponentScan(basePackages = {"com.priusis.bmis.app.web.controller"})
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("dr-erp接口文档")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
controller:
@RestController
@RequestMapping("/app/home")
@Api(value = "AppHomeController", description = "主页相关信息接口")
public class AppHomeController {
@Resource
private IAppHomeService appHomeService;
@GetMapping("/loadInfo")
@ApiOperation(value = "", notes = "加载主页信息")
public Result<HomeVo> loadHomeInfo() {
return Result.success(appHomeService.loadHomeInfo());
}
/**
* @ApiModelProperty("物料信息")
* private List<ItemAppVo> items;
* @param limit
* @return
*/
@GetMapping("/loadItem")
@ApiOperation(value = "", notes = "加载热销商品信息")
public Result<List<ItemAppVo>> loadItem(@ApiParam(required = false, value = "每页商品数量")
@RequestParam(required = false, defaultValue = PageConstant.APP_PAGE_SIZE) Integer limit) {
return Result.success(appHomeService.loadItem(limit));
}
}
vo:
@Data
public class HomeVo {
@ApiModelProperty("轮播图")
private String[] images;
@ApiModelProperty("公司名称")
private String companyName;
@ApiModelProperty("公司地址")
private String companyAddress;
@ApiModelProperty("公司邮箱")
private String companyEmail;
@ApiModelProperty("公司电话")
private String companyTel;
}