Is there a way to use Swagger just for validation without using the whole framework?

十年热恋 提交于 2019-12-04 05:06:51

问题


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?

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


回答1:


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

  • 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.

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



来源:https://stackoverflow.com/questions/36488357/is-there-a-way-to-use-swagger-just-for-validation-without-using-the-whole-framew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!