Java 招摇过市-使用@ApiParam和@CookieParam发布

Java 招摇过市-使用@ApiParam和@CookieParam发布,java,cookies,swagger,param,Java,Cookies,Swagger,Param,我正在使用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 = "

我正在使用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)
现在,实际的招摇过市用户界面呈现得很好,实际上……在本例中,它甚至用“101”填充默认值。问题是,当我单击“试用”时,品牌参数始终为空

好像我错过了一些简单的事情…有什么想法吗


非常感谢

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);
    }
}
import org.springframework.core.annotation.Order;
导入org.springframework.stereotype.Component;
导入org.springframework.web.bind.annotation.CookieValue;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spi.service.ParameterBuilderPlugin;
导入springfox.documentation.spi.service.contexts.ParameterContext;
导入java.lang.annotation.annotation;
导入java.util.List;
导入静态springfox.documentation.swagger.common.SwaggerPluginSupport.swagger\u PLUGIN\u顺序;
导入静态springfox.documentation.swagger.common.SwaggerPluginSupport.pluginDoesApply;
/*
*自定义招摇过市配置以支持请求中的cookie
*
*由Saransh Bansal于2021年3月11日13:02创建
*/
@组成部分
@订单(招摇过市订单+1000)
公共类SwaggerCustomParameterBuilderPlugin实现ParameterBuilderPlugin{
@凌驾
public void apply(参数上下文){
if(isCookieValue(上下文)){
parameterBuilder().parameterType(“cookie”);
}
}
私有布尔值IsCookeValue(ParameterContext上下文){

列出

关于这项工作的任何建议?我的项目中有很多@CookieParam。除非所有这些都可以表示为标题参数,否则没有。