java Swagger 示例帖子正文 - 如何显示 JSON 正文 - Swagger-注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48039876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swagger example post body - how to show JSON Body- Swagger-annotations
提问by Sum
Requirement: I have a POST method which takes the input JSON as a String and passes it to another microservice. I don't want to create an Object (Bean) of this input JSON.
要求:我有一个 POST 方法,它将输入的 JSON 作为字符串并将其传递给另一个微服务。我不想创建此输入 JSON 的对象(Bean)。
method:
方法:
@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){
}
Problem: The generated Swagger doesn't show the input as a JSON model where all the JSON attributes are displayed.
问题:生成的 Swagger 没有将输入显示为显示所有 JSON 属性的 JSON 模型。
Expectation: I want to display my Swagger Something like this :
期望:我想显示我的 Swagger 像这样的东西:
Definately I am missing the key thing. Any thoughts?
毫无疑问,我错过了关键的事情。有什么想法吗?
回答by Andrei Sfat
If changing from String
to a concrete object is not okay (although that's what I would recommend you to do since it's cleaner), you can try using @ApiImplicitParams
(check out their documentation)
如果从更改String
为具体对象是不行的(尽管我建议您这样做,因为它更干净),您可以尝试使用@ApiImplicitParams
(查看他们的文档)
@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@ApiImplicitParams({
@ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){
}
(not sure if you still need the @Apiparam(name="JSONrequest", required = true)
bit from the method parameter)
(不确定您是否仍然需要@Apiparam(name="JSONrequest", required = true)
方法参数中的位)