如何设置 Swagger 忽略 @Asynchronous jax-rs bean 方法中的 @Suspished AsyncResponse?

2024-03-09

Swagger-Core 似乎将 @Suspished 最终 AsyncResponse asyncResponse 成员解释为请求正文参数。这显然不是有意的,也不是事实。

我想告诉 swagger-core 忽略此参数并将其从 api 文档中排除。有任何想法吗?

这就是我的代码的样子:

@Stateless
@Path("/coffee")
@Api(value = "/coffee", description = "The coffee service.")
public class CoffeeService
{
    @Inject
    Event<CoffeeRequest> coffeeRequestListeners;

    @GET
    @ApiOperation(value = "Get Coffee.", notes = "Get tasty coffee.")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "Beans not found."),
            @ApiResponse(code = 500, message = "Something exceptional happend.")})
    @Produces("application/json")
    @Asynchronous
    public void makeCoffee( @Suspended final AsyncResponse asyncResponse,

                             @ApiParam(value = "The coffee type.", required = true)
                             @QueryParam("type")
                             String type)
    {
        coffeeRequestListeners.fire(new CoffeeRequest(type, asyncResponse));
    }
}

更新:基于答案的解决方案

public class InternalSwaggerFilter implements SwaggerSpecFilter
{
    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
        if( parameter.paramAccess().isDefined() && parameter.paramAccess().get().equals("internal") )
            return false;
        return true;
    }
}

FilterFactory.setFilter(new InternalSwaggerFilter());

修改后的示例代码片段

...
@Asynchronous
public void makeCoffee( @Suspended @ApiParam(access = "internal") final AsyncResponse asyncResponse,...)
...

快进到 2016 年,swagger-springmvc 被替换为春狐 https://github.com/springfox/springfox(文档可用here https://springfox.github.io/springfox/docs/current/)。 springfox 中可以忽略参数,但由于某种原因没有记录:

替代方案1:全局忽略类型或带注释的类型.ignoredParameterTypes(...)在案卷配置中:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .host(reverseProxyHost)
        .useDefaultResponseMessages(false)
        .directModelSubstitute(OffsetDateTime.class, String.class)
        .directModelSubstitute(Duration.class, String.class)
        .directModelSubstitute(LocalDate.class, String.class)
        .forCodeGeneration(true)
        .globalResponseMessage(RequestMethod.GET, newArrayList(
            new ResponseMessageBuilder()
                .code(200).message("Success").build()
            )
        .apiInfo(myApiInfo())
        .ignoredParameterTypes(AuthenticationPrincipal.class, Predicate.class, PathVariable.class)
        .select()
            .apis(withClassAnnotation(Api.class))
            .paths(any())
        .build();
}

替代方案2: Use @ApiIgnore-注释忽略方法中的单个参数:

@ApiOperation(value = "User details")
@RequestMapping(value = "/api/user", method = GET, produces = APPLICATION_JSON_UTF8_VALUE)
public MyUser getUser(@ApiIgnore @AuthenticationPrincipal MyUser user) {
    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置 Swagger 忽略 @Asynchronous jax-rs bean 方法中的 @Suspished AsyncResponse? 的相关文章

  • DDD和应用层

    我在DDD中添加 Stateful Stateless WebService等是应用层 应用服务 吗 从下面的链接来看 这似乎是正确的 第二个问题 我创建了一个存储库类 所有涉及存储库的方法调用都应该包装在应用程序服务中吗 或者我可以直接在
  • Apache AVRO 与休息

    我正在评估将 Apache AVRO 用于我的 Jersey REST 服务 我将 Springboot 与 Jersey REST 结合使用 目前我接受 JSON 作为输入 并使用 Jackson 对象映射器将其转换为 Java Pojo
  • JAX-RS自定义ExceptionMapper不拦截RuntimeException

    我想包裹底层RuntimeExceptions自定义 json 格式 使得 servlet 容器不会将堆栈跟踪转储到客户端 我关注这个问题 使用 XML 或 JSON 的 JAX RS Jersey 自定义异常 https stackove
  • 在weblogic上升级jax-rs共享库

    通常weblogic 12c不支持jax rs 2 0 但是通过weblogic本身附带的jax rs共享库的帮助 可以将jax rs版本从1 1升级到2 0 问题是库实现是 jersey 2 5 它不能满足我的需求 我在 weblogic
  • Swagger - 时间戳奇怪的表示

    我有 Spring Boot 控制器 为了方便 API 参考 我使用了 Swagger 关于问题timestamp 为什么在生成的文档示例中看起来像 timestamp date 0 day 0 hours 0 minutes 0 mont
  • JBoss AS 6 中的 QuartzScheduler 注入

    如何将 QuartzScheduler 服务注入 JBoss AS 6 上的无状态 bean 中 Quartz 服务在 JBoss AS 6 启动期间启动 00 58 38 025 INFO QuartzScheduler Schedule
  • 提供 REST Web 服务的 JEE6 企业应用程序应该如何组织?

    从一个月前开始 我正在努力学习宁静的网络服务 现在我已经练习了语法并且理解了概念 我决定制作一个非常简单的企业应用程序 其中包括 EJB JPA 和 REST 我正在付出巨大的努力来尝试了解组织这种系统的最佳方式是什么 如果在该领域有经验的
  • JAX-RS:是否有用于根元素、列表的一部分、Web 服务中的参数的 json 序列化器

    目前我正在设计一个 RESTful API 并使用 JAX RS 作为后端 许多响应具有以下简单形式 someList item1 item2 itemn 重要的是数组的字段名称 客户端需要它 EmberJS 数据 我尝试使用标准 Resp
  • Swagger WebApi 在构建时创建 json

    有什么方法可以在我的 Web api 的构建任务期间创建 swagger json 吗 我想使用 json 将其输入代码生成器并生成打字稿定义文件 非常欢迎任何帮助 我在用着虚张声势 AspNetCore Cli 注意 我使用的是 NET
  • Camel JAX-RS 和跨域请求

    我希望能够在我的本地 Camel 实例上执行 HTTP 请求 仅出于开发目的 我知道这是不好的做法 现在 我坚持 Origin http localhost 8000 is not allowed by Access Control All
  • swagger-ui 无法使用自定义 XML ObjectMapper

    我正在开发一个应该启用 swagger ui 的 Spring Boot 应用程序 访问时http localhost 8080 swagger ui html http localhost 8080 swagger ui html有一个错
  • 如何在 OpenAPI (Swagger) 中参数化 API 基本路径?

    我有一个像这样的网址 id idnumber status 在这个网址中 id idnumber 是 API 基本路径 并且 status是资源 我知道 OpenAPI Swagger 允许在路径中使用参数 如下所示 paths id nu
  • 具有更多注入 EJB 实例的无状态 EJB

    我知道无状态 EJB 存储在池中并根据需要进行实例化 我的问题是 当存在更多 EJB 依赖项时会发生什么 例如如下所示 Remote Stateless public class Master EJB EJB private EJB A e
  • 线程“main”中出现异常 java.lang.NoClassDefFoundError:无法初始化类 com.sun.jersey.core.header.MediaTypes

    我正在尝试运行一个球衣客户端并面临这个问题 WS级 import javax ws rs GET import javax ws rs Path import javax ws rs Produces import javax ws rs
  • 如何在 swagger 规范中表示十进制浮点数?

    我想在我的 api 文档中用 2 位小数表示小数点 用 1 位小数表示 我正在使用 swagger 2 0 规范中是否有内置定义的类型或任何其他 圆形 参数 或者我唯一的选择是使用 x 扩展 OpenAPI fka Swagger 规范使用
  • 无法查找 Websphere 8.5 中是否启用了 SSL

    我编写了一个简单的 ejb 瘦客户端来查找 IBM WebSphere 8 5 中部署的 bean 当服务器上未启用 SSL 时 我能够成功查找 bean 但是一旦启用 SSL 我就开始收到下面提到的异常 This is how I ena
  • 如何从不同 JVM 中的独立 java 程序调用 EJB

    我使用具有 glassfish 服务器的 NETbeans IDE 开发了 EJB 我可以从独立的java调用它 只要这个java项目是在NETbeans下开发并在相同的JRE下运行的 但我需要找出如何在不同的 jre 中运行它 我在这方面
  • 如何在 OpenAPI (Swagger) 中描述这个 POST JSON 请求正文?

    我有一个使用以下 JSON 请求正文的 POST 请求 如何使用 OpenAPI Swagger 描述此请求正文 testapi testapiContext messageId kkkk8 messageDateTime 2014 08
  • Jersey 将 Weld 托管 bean 注入 ConstraintValidator

    我已经花了几个小时寻找解决方案来解决我的问题 但我无法让它发挥作用 我想将 Weld 管理的服务注入 ConstraintValidator 中 该 ConstraintValidator 用于验证发布到 JAX RS Rest Servi
  • 如何更改 Swagger-ui URL 前缀?

    我正在使用 Springfox Swagger2 和 Spring boot 1 5 9 我可以通过此链接访问 swagger UI http localhost 8090 swagger ui html http localhost 80

随机推荐