Swagger - 使用@ApiParam 和@CookieParam 的问题

Swagger - Issue using @ApiParam with @CookieParam

我正在使用 Swagger 1.3.10 并试图让 Swagger UI 在功能上接受 REST 服务的 cookie 参数。这是我的 Java 代码示例:

    public Response getUserInfo(
    @Context HttpHeaders headers, 
    @ApiParam(value="Enter brand code as an Integer", defaultValue="101", required=true) @CookieParam(value = "userBrand") String brand)

现在实际的 Swagger UI 实际上渲染得很好......在这种情况下它甚至用“101”填充默认值。问题是当我单击 "Try it Out" 时,品牌参数始终显示为 null。

好像我在这里遗漏了一些简单的东西……有什么想法吗?

非常感谢!

Swagger 并不真正支持 Cookie 参数。 Swagger-core 将它们生成为 cookie 参数,但其他工具不支持它们,因为这不是规范的官方部分。

不支持开箱即用的 Cookie 参数。相反,您需要为此编写自己的处理程序。看这里:

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CookieValue;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ParameterBuilderPlugin;
import springfox.documentation.spi.service.contexts.ParameterContext;

import java.lang.annotation.Annotation;
import java.util.List;

import static springfox.documentation.swagger.common.SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER;
import static springfox.documentation.swagger.common.SwaggerPluginSupport.pluginDoesApply;

/*
 * Custom swagger configuration to support Cookies in requests
 *
 * Created by Saransh Bansal on 11/03/2021, 13:02
 */
@Component
@Order(SWAGGER_PLUGIN_ORDER + 1000)
public class SwaggerCustomParameterBuilderPlugin implements ParameterBuilderPlugin {

    @Override
    public void apply(ParameterContext context) {
        if (isCookieValue(context)) {
            context.parameterBuilder().parameterType("cookie");
        }
    }

    private boolean isCookieValue(ParameterContext context) {
        List<Annotation> annotations = context.resolvedMethodParameter().getAnnotations();
        return annotations.stream().anyMatch(annotation -> annotation.annotationType() == CookieValue.class);
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return pluginDoesApply(documentationType);
    }
}

代码提供者:https://github.com/mwinteringham