swagger2初学配置及文件上传如何配置

swargger2实现API接口测试

1.maven配置:

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.2.2</version>
    </dependency>

2.在Application同级目录下新建Swagger2类:


@Configuration
@EnableSwagger2   //启用swagger2
public class Swagger2 {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.yonyou.myspringboot"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Spring Boot中使用Swagger2构建RESTful APIs")
        .description("spring boot 个人测试")
//        .termsOfServiceUrl("")
        .contact("程序猿DD")
        .version("1.0")
        .build();
  }

}

文件上传配置:

  @ApiOperation(value = "发票上传", notes = "发票上传")
  @ApiImplicitParam(name = "assetId", value = "凭证ID", required = true, dataType = "String", paramType = "String")
  @RequestMapping(value = "uploadingFiles", method = RequestMethod.POST , consumes = "multipart/form-data")
  @ResponseBody
  public BaseResult uploadFile(String assetId, @ApiParam(value = "上传的文件", required = true) @RequestParam("jarFile") MultipartFile jarFile) throws IOException {
    if(jarFile != null){
      String name = jarFile.getName();
      long size = jarFile.getSize();
      log.debug("assetId:{},name:{}, size:{}", assetId, name, size);
      System.out.println(new String(jarFile.getBytes()));
      return BaseResult.OK(new String(jarFile.getBytes()));
    }
    return BaseResult.Error();
  }

rest风格接口配置:

 @ApiOperation(value="获取用户信息", notes="根据id获取用户信息", httpMethod = "GET")
  @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
  @RequestMapping(path = "consumingRest/{id}")
  public User getUser(@PathVariable int id){
    User user = new User();
    user.setId(id);
    user.setAge(20);
    user.setName("张三");
    return user;
  }

dataType = “Integer”, paramType = “path” 否则调用时出现不能转换的错误

需要源码的可以评论留下你的微信号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值