1、maven导入Swagger
<!--swagger2 -->
<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>
2、创建Swagger2配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.google.common.base.Predicates;
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;
@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig implements WebMvcConfigurer { //继承WebMvcConfigurer来拦截包
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 开放文档的controller包名,可以添加多个.apis
.apis(RequestHandlerSelectors.basePackage("包路径.controller"))
//排除包
.apis(Predicates.not(RequestHandlerSelectors.basePackage("包路径.controller.exclude")))
//线上环境禁用文档,如果是线上环境,添加路径过滤,设置为全部都不符合
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("接口列表 v1.0.0") // 任意,请稍微规范点
.description("接口测试") // 任意,请稍微规范点
.termsOfServiceUrl("http://url/swagger-ui.html") // 将“url”换成自己的ip:port
.version("1.0.0").build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
3、用注解形式应用到Contrlller类
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
@RequestMapping("/exc/pushAccept")
@Api(value = "类说明")
public class PushOldAcceptContrlller {
@ResponseBody //不返回页面时带上,反之不带
@PostMapping(value = "/importExcel",consumes = "multpart/*",headers = {"content-type=multipart/form-data","content-type=application/json"})
@ApiOperation(value = "上传",notes = "上传")
public String importExcel(@ApiParam(value = "文件",required = true) MultipartFile files) {
if (!files.isEmpty()) {
//文件真实名,带后缀
String fName = files.getOriginalFilename();
return "json串";
}
}
}
4、注解详情
swagger常用注解说明(转载)