有没有办法只使用 Swagger 进行验证而不使用整个框架? [英] Is there a way to use Swagger just for validation without using the whole framework?

查看:19
本文介绍了有没有办法只使用 Swagger 进行验证而不使用整个框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个实现 JSON HTTP API 的现有 Java 服务,并且我想添加一个 Swagger 模式并自动验证针对它的请求和响应,而不需要对服务进行重组以使用 Swagger 框架/代码生成.有什么东西可以提供 Java API,我可以绑定并传递有关请求/响应的信息以进行验证?

Suppose I have an existing Java service implementing a JSON HTTP API, and I want to add a Swagger schema and automatically validate requests and responses against it without retooling the service to use the Swagger framework / code generation. Is there anything providing a Java API that I can tie into and pass info about the requests / responses to validate?

(仅使用 JSON 模式验证器将意味着在 Swagger 中手动实现许多附加功能.)

(Just using a JSON schema validator would mean manually implementing a lot of the additional features in Swagger.)

推荐答案

我认为没有什么可以单独完成,但您可以通过以下方式轻松完成:

I don't think there's anything ready to do this alone, but you can easily do this by the following:

  • Swagger Inflector 项目.您可以使用它来验证入站和出站负载
  • 为您的请求/响应定义分配架构部分.这意味着您需要将 JSON 架构的特定部分分配给您的操作
  • 为您的 API 创建一个过滤器以获取有效负载并使用架构
  • Grab the SchemaValidator from the Swagger Inflector project. You can use this to validate inbound and outbound payloads
  • Assign a schema portion to your request/response definitions. That means you'll need to assign a specific section of the JSON schema to your operations
  • Create a filter for your API to grab the payloads and use the schema

这将让您轻松查看负载是否与预期结构匹配.

That will let you easily see if the payloads match the expected structure.

当然,这一切都是使用 Inflector 自动为您完成的,但应该有足够的原始组件来帮助您在自己的实现中做到这一点

Of course, this is all done for you automatically with Inflector but there should be enough of the raw components to help you do this inside your own implementation

这篇关于有没有办法只使用 Swagger 进行验证而不使用整个框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆