Spring Boot + Swagger + Swagger UI和@RequestBody的数据类型为String

时间:2016-08-04 19:43:25

标签: java rest spring-boot swagger swagger-ui

我使用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");
}

@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;
    }
}

我使用MessageDto的全名或设置@ApiModel的正确值发现了很多修复,但似乎没有任何效果。

我在https://github.com/larmic/SpringBootAndSwaggerUI

创建了一个完整的示例

也许有人可以提供帮助。

1 个答案:

答案 0 :(得分:8)

这似乎是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");
}