Swagger2接口注释参数使用数组

allowMultiple = true, paramType = "query", dataType = "string"

输出的就是这样的:Array[string]

这种有个好处,Swagger2前台采用的多行文本框进行输入。

在传统模式来说,我们一般是使用逗号数组,但是这种很多不靠谱的情况,比如中文逗号,代码要做切割和解析、转换等等。以上这种方式也是多一种友好方式的选择。

 

参考:

https://github.com/springfox/springfox/issues/902

转载于:https://www.cnblogs.com/EasonJim/p/8018311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger2可以通过设置SecurityScheme来设置authorize。具体步骤如下: 1. 定义SecurityScheme 在Swagger2的配置类中,我们需要定义一个SecurityScheme,用于设置authorize。可以使用ApiKey、BasicAuth、OAuth2等多种认证方式,这里以OAuth2为例: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .securitySchemes(Arrays.asList(securityScheme())) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private SecurityScheme securityScheme() { GrantType grantType = new AuthorizationCodeGrantBuilder() .tokenEndpoint(new TokenEndpoint("http://localhost:8080/oauth/token", "oauthtoken")) .tokenRequestEndpoint( new TokenRequestEndpoint("http://localhost:8080/oauth/authorize", "client_id", "client_secret")) .build(); return new OAuthBuilder().name("oauth2") .grantTypes(Arrays.asList(grantType)) .scopes(Arrays.asList(scopes())) .build(); } private AuthorizationScope[] scopes() { AuthorizationScope[] scopes = { new AuthorizationScope("read", "for read operations"), new AuthorizationScope("write", "for write operations") }; return scopes; } } ``` 2. 设置SecurityRequirement 在接口文档中,需要通过SecurityRequirement来指定哪些接口需要认证,哪些不需要认证。可以设置全局的SecurityRequirement,也可以为每个接口单独设置SecurityRequirement。这里以全局设置为例: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .securitySchemes(Arrays.asList(securityScheme())) .securityContexts(Arrays.asList(securityContext())) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private SecurityScheme securityScheme() { //... } private SecurityContext securityContext() { return SecurityContext.builder() .securityReferences(Arrays.asList(new SecurityReference("oauth2", scopes()))) .forPaths(PathSelectors.any()) .build(); } private AuthorizationScope[] scopes() { //... } } ``` 这样,就可以在接口文档中看到Authorize按钮了。点击按钮后,会弹出一个对话框,要求输入OAuth2的认证信息,然后就可以访问需要认证的接口了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值