简体   繁体   English

如何在swagger中隐藏参数?

[英]How to hide a parameter in swagger?

did anyone succeed to hide a parameter from generated documentation?有没有人成功地从生成的文档中隐藏参数? I found an issue here , but using @ApiParam(access="internal", required=false) before @HeaderParam did not seem to work.我在这里发现了一个问题,但是在@HeaderParam之前使用@ApiParam(access="internal", required=false)似乎不起作用。

Hope this helps.希望这可以帮助。

For Fields对于字段

@ApiModelProperty(required = false, hidden = true)
private String hiddenProperty

For Apis用于蜜蜂

@ApiIgnore
public class MyApi {}

For Parameters对于参数

public void getApi(@ApiIgnore String param){}

@ApiModelProperty(hidden="true")
public String paramInsideClass

Ok, looking at the unit tests helped.好的,查看单元测试有帮助。 First you need to define a filter:首先你需要定义一个过滤器:

import com.wordnik.swagger.core.filter.SwaggerSpecFilter
import com.wordnik.swagger.model.{Parameter, ApiDescription, Operation}
import java.util

class MySwaggerSpecFilter extends SwaggerSpecFilter{
  override def isOperationAllowed(operation: Operation, api: ApiDescription, params: util.Map[String, util.List[String]], cookies: util.Map[String, String], headers: util.Map[String, util.List[String]]): Boolean = true

  override def isParamAllowed(parameter: Parameter, operation: Operation, api: ApiDescription, params: util.Map[String, util.List[String]], cookies: util.Map[String, String], headers: util.Map[String, util.List[String]]): Boolean = {
    if(parameter.paramAccess == Some("internal")) false
    else true
  }
}

And then enable it in web.xml然后在web.xml启用它

    <servlet>
        <servlet-name>DefaultJaxrsConfig</servlet-name>
        <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        ...
        <init-param>
            <param-name>swagger.filter</param-name>
            <param-value>com.example.MySwaggerSpecFilter</param-value>
        </init-param>
    </servlet>

With swagger-springmvc ( https://github.com/springfox/springfox ) at the moment there's no way to use SwaggerSpecFilter.使用 swagger-springmvc ( https://github.com/springfox/springfox ) 目前无法使用 SwaggerSpecFilter。 But it respects @ApiIgnore annotation - it can be applied to method parameter which shouldn't appear in generated metadata.但它尊重 @ApiIgnore 注释 - 它可以应用于不应出现在生成的元数据中的方法参数。

如果您使用io.swagger.v3 ,则应使用@Parameter(hidden = true)@Parameter(hidden = true)https://springdoc.org/migrating-from-springfox.html迁移指南中所述

注释@ApiParam(hidden = true)为我解决了问题。

def myFunc(@ApiParam(hidden = true) i: Int) = {...}

In sprigfox-swagger2 implementation there is an annotation @ApiModelProperty that does this.sprigfox-swagger2实现中,有一个注释@ApiModelProperty可以做到这一点。

Example:例子:

@ApiModelProperty(required = false, hidden = true)
private String internallyUsedProperty;

答案使用.ignoredParameterTypes@ApiIgnore描述了 springfox 中的当前解决方案

You could annotate your fields with:您可以使用以下方法注释您的字段:

@Schema(description = "foo bar.", required = false, hidden = true, example = "bar")
private String fooDtoField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM