swagger基础运用

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常用注解说明(转载)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值