.useDefaultResponseMessages(false) .globalOperationParameters(getGlobalParameters())含义
时间: 2024-05-17 07:15:34 浏览: 9
`.useDefaultResponseMessages(false)` 表示禁止使用 Swagger2 默认的响应信息,例如 HTTP 状态码、错误描述等。你可以通过自定义响应信息的方式替换默认的信息。
`.globalOperationParameters(getGlobalParameters())` 表示添加全局的操作参数,例如在所有接口中都添加一个名为 "token" 的请求头参数。在你提供的配置中,使用 `ParameterBuilder` 构建了一个 "token" 参数,并将其添加到了全局操作参数列表中。这样就可以在所有接口中使用 "token" 请求头参数了。
相关问题
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket templateApi() { ParameterBuilder tokenPar = new ParameterBuilder(); tokenPar.name("token").description("token令牌") .modelRef(new ModelRef("string")).parameterType("header").required(false).build(); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .useDefaultResponseMessages(false) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .build().globalOperationParameters(Lists.newArrayList(tokenPar.build())); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("佛山市协同应急指挥系统 Rest API 文档") .version("1.0") .build(); } }
这段代码是一个 Swagger2 的配置类,其中配置了一个 templateApi 的 Docket Bean。在该 Bean 中,我们使用 ParameterBuilder 构建了一个名为 token 的 header 参数,并将其作为全局操作参数添加到 Docket 中。这样,对于所有接口,都会自动添加一个 token 参数。
另外,该配置类还设置了 API 文档的标题和版本号,并指定只扫描标注了 @RestController 注解的类。
需要注意的是,这里的 token 参数是可选的,即 required(false),如果需要对所有接口进行认证,应该将其设置为 required(true)。
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
这个错误提示是因为 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` 类型的请求了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)