【问题标题】:Request validation using swagger使用 swagger 请求验证
【发布时间】:2018-01-19 09:55:58
【问题描述】:

我需要在 java 中使用 swagger(YAML 文件)验证传入的 REST 请求。所以任何人都可以帮我解决这个问题。 提前致谢。

【问题讨论】:

标签: yaml swagger-2.0


【解决方案1】:

SwaggerValidationInterceptor

有一个接受 Byte 数组的构造函数。见下文:

public SwaggerValidationInterceptor(final EncodedResource swaggerInterface) throws IOException {
        this(new SwaggerRequestValidationService(swaggerInterface));
    }

因此,您可以通过将 yaml 文件转换为字节数组,然后将其传递给 Swagger 来实现。

要将其转换为字节数组,您可以按照以下方法:

public SwaggerValidationInterceptor swaggerValidationInterceptor(Resource swaggerDescriptor) throws IOException {
        final byte[] yamlPathAsBytes = swaggerDescriptor.getFile().getAbsolutePath().getBytes(UTF_8);
        return new SwaggerValidationInterceptor(
                new EncodedResource(new ByteArrayResource(yamlPathAsBytes), UTF_8));
    }

【讨论】:

  • 只删除名称 SwaggerValidationInterceptor 并没有多大帮助。这个拦截器属于 swagger-request-validator 库(链接?!)。它是 Spring MVC 应用程序的适配器。不具体的问题没有提到任何春天。所以这只是一个猜测。
猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 2020-05-08
  • 2023-03-22
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多