swagger文档请求参数为文件属性添加选择文件按钮

发布时间 2023-07-26 17:15:41作者: 快乐小洋人

在编写接口时,将所有参数封装到实体中,其中就包含MultipartFile类型的参数

eg:

参数实体:User

@ApiModel(value = "用户实体类")
@Data
public class User {
    @ApiModelProperty(value = "用户名")
    public String userName;
    @ApiModelProperty(value = "密码")
    public String password;
    @ApiModelProperty(value = "照片")
    public MultipartFile file;
}

接口:


@ApiOperation(value = "接口方法描述4")//这是给接口方法添加注释的
@PostMapping(value = "/insertUser")
public String insertUser(User user){
   return "我是用户="+user.getUserName();
}

显示正常时:

调用接口:

发现文件为,null

解决方式:@ApiImplicitParams(dataType="__file",paramType="form")设置参数类型和传参方式

参数支持类型:


package springfox.documentation.schema;
 
import com.fasterxml.classmate.ResolvedType;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.multipart.MultipartFile;
 
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Currency;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
 
import static com.google.common.collect.Sets.*;
 
public class Types {
  private Types() {
    throw new UnsupportedOperationException();
  }
 
  private static final Set<String> baseTypes = newHashSet(
      "int",
      "date",
      "string",
      "double",
      "float",
      "boolean",
      "byte",
      "object",
      "long",
      "date-time",
      "__file",
      "biginteger",
      "bigdecimal",
      "uuid");
  private static final Map<Type, String> typeNameLookup = ImmutableMap.<Type, String>builder()
      .put(Long.TYPE, "long")
      .put(Short.TYPE, "int")
      .put(Integer.TYPE, "int")
      .put(Double.TYPE, "double")
      .put(Float.TYPE, "float")
      .put(Byte.TYPE, "byte")
      .put(Boolean.TYPE, "boolean")
      .put(Character.TYPE, "string")
 
      .put(Date.class, "date-time")
      .put(java.sql.Date.class, "date")
      .put(String.class, "string")
      .put(Object.class, "object")
      .put(Long.class, "long")
      .put(Integer.class, "int")
      .put(Short.class, "int")
      .put(Double.class, "double")
      .put(Float.class, "float")
      .put(Boolean.class, "boolean")
      .put(Byte.class, "byte")
      .put(BigDecimal.class, "bigdecimal")
      .put(BigInteger.class, "biginteger")
      .put(Currency.class, "string")
      .put(UUID.class, "uuid")
      .put(MultipartFile.class, "__file")
      .build();
 
  public static String typeNameFor(Type type) {
    return typeNameLookup.get(type);
  }
 
  public static boolean isBaseType(String typeName) {
    return baseTypes.contains(typeName);
  }
 
  public static boolean isBaseType(ResolvedType type) {
    return baseTypes.contains(typeNameFor(type.getErasedType()));
  }
 
  public static boolean isVoid(ResolvedType returnType) {
    return Void.class.equals(returnType.getErasedType()) || Void.TYPE.equals(returnType.getErasedType());
  }
}

通过上面Types类可以看到设置dataType为__file,paramType为form即可设置参数类型和传参方式

@ApiOperation(value = "接口方法描述4")//这是给接口方法添加注释的
@PostMapping(value = "/insertUser")
@ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType="__file", name = "file1",value = "照片啊", required = false)})
public String insertUser(User user){
    return "我是用户="+user.getUserName();
}

成功接收到

注:我这里用的swagger版本是2.9.2,此版本使用"__file"设置参数为文件对象,低版本中使用的是"file"

swagger 注解:

@Api:用在请求的类上,表示对类的说明
    tags="说明该类的作用,可以在UI界面上看到的注解"
    value="该参数没什么意义,在UI界面上也看到,所以不需要配置"
 
 
@ApiOperation:用在请求的方法上,说明方法的用途、作用
    value="说明方法的用途、作用"
    notes="方法的备注说明"
 
 
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
    @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
        name:参数名
        value:参数的汉字说明、解释
        required:参数是否必须传
        paramType:参数放在哪个地方
            · header --> 请求参数的获取:@RequestHeader
            · query --> 请求参数的获取:@RequestParam
            · path(用于restful接口)--> 请求参数的获取:@PathVariable
            · body(不常用)
            · form(不常用)    
        dataType:参数类型,默认String,其它值dataType="Integer"       
        defaultValue:参数的默认值
 
 
@ApiResponses:用在请求的方法上,表示一组响应
    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
        code:数字,例如400
        message:信息,例如"请求参数没填好"
        response:抛出异常的类
 
 
@ApiModel:用于响应类上,表示一个返回响应数据的信息
            (这种一般用在post创建的时候,使用@RequestBody这样的场景,
            请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    @ApiModelProperty:用在属性上,描述响应类的属性

原文章地址:

https://www.freesion.com/article/99731558105/