java Spring Boot + Swagger + Swagger UI 和 @RequestBody 具有数据类型 String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38776088/
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
Spring Boot + Swagger + Swagger UI and @RequestBody has data type String
提问by Lars Michaelis
I've got a problem using Spring Boot 1.4 and Swagger and Swagger UI. When using @RequestBody parameter is displaying as data type string. This does not seems correct.
我在使用 Spring Boot 1.4 和 Swagger 以及 Swagger UI 时遇到了问题。当使用@RequestBody 参数显示为数据类型字符串时。这似乎不正确。
@ApiOperation(value = "simple message resource")
@ApiImplicitParams({
@ApiImplicitParam(name = "message", value = "Message to send", required = true, dataType = "com.larmic.springboot.swagger.rest.dto.MessageDto", paramType = "body")
})
@RequestMapping(value = "/api/message", method = RequestMethod.POST,
consumes = {"application/json", "application/xml"})
public void sendMessage(@RequestBody MessageDto message) {
System.out.println("ping");
}
and
和
@XmlRootElement(name = "MessageDto")
@XmlAccessorType(XmlAccessType.FIELD)
@ApiModel(value = "MessageDto", description = "TODO")
public class MessageDto {
@ApiModelProperty(value = "Message content text", required = true, example = "some demo message")
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
I've found a lot of fixes using full name of MessageDto or setting correct value of @ApiModel but nothing seems to work.
我找到了很多使用 MessageDto 全名或设置正确值 @ApiModel 的修复程序,但似乎没有任何效果。
I've created a full example here https://github.com/larmic/SpringBootAndSwaggerUI
我在这里创建了一个完整的例子https://github.com/larmic/SpringBootAndSwaggerUI
Maybe someone can help.
也许有人可以提供帮助。
回答by g00glen00b
This appears to be a bug in Springfox (#1344). You could work around it by not using @ApiImplicitParams
, but by annoting your method parameter itself with the @ApiParam
annotation:
这似乎是 Springfox ( #1344) 中的一个错误。您可以通过不使用来解决它@ApiImplicitParams
,而是通过使用@ApiParam
注释来注释您的方法参数本身:
@ApiOperation(value = "simple message resource")
@RequestMapping(value = "/api/message", method = RequestMethod.POST,
consumes = {"application/json", "application/xml"})
public void sendMessage(@ApiParam(name = "message", value = "Message to send", required = true) @RequestBody MessageDto message) {
System.out.println("ping");
}