有没有一种方法可以将Swagger仅用于验证而无需使用整个框架? [英] Is there a way to use Swagger just for validation without using the whole framework?

查看:70
本文介绍了有没有一种方法可以将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:

  • SchemaValidator .java"rel =" nofollow> 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天全站免登陆