springboot项目中接口代码是 @PostMapping("/addAdvert") @ApiOperation(value = "新增广告") @ApiImplicitParams({ @ApiImplicitParam(name = "advertDto", value = "广告对象", dataType = "AdvertDTO", paramType = "body"), @ApiImplicitParam(name = "file", value = "文件", dataType = "__file", paramType = "form") }) public JsonData<String> addAdvert(@RequestBody AdvertDTO advertDto, MultipartFile file),配置是 @Bean public Docket createRestApi(Environment environment) { // 设置要显示swagger的环境 Profiles of = Profiles.of("dev", "test"); // 判断当前是否处于该环境 // 通过 enable() 接收此参数判断是否要显示 boolean b = environment.acceptsProfiles(of); ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false) .defaultValue("38750c3c941c20bc1eaf204e5a868ecb").build(); pars.add(tokenPar.build()); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ruifan.taihaolian")) .paths(PathSelectors.any()) .build() .consumes(Sets.newHashSet("multipart/form-data")) .produces(Sets.newHashSet("application/json")) .directModelSubstitute(byte[].class, String.class) .useDefaultResponseMessages(false) .globalOperationParameters(pars); } Swagger2调用该接口报错Content type 'multipart/form-data;boundary=----WebKitFormBoundaryubXGS6xpNFd3ij0f;charset=UTF-8' not supported
时间: 2024-02-02 15:03:24 浏览: 75
springboot项目中json导出成标准接口文档到word(swagger样式)
5星 · 资源好评率100%
这个错误提示是因为 Swagger2 默认不支持 `multipart/form-data` 类型的请求,需要添加相应的配置来支持。你可以尝试添加以下配置到 `createRestApi` 方法中:
```
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("v1")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ruifan.taihaolian"))
.paths(PathSelectors.any())
.build()
.consumes(Sets.newHashSet("application/json", "multipart/form-data")) // 添加 support for multipart/form-data
.produces(Sets.newHashSet("application/json"))
.directModelSubstitute(byte[].class, String.class)
.useDefaultResponseMessages(false);
}
```
这样就能够支持 `multipart/form-data` 类型的请求了。
阅读全文