【问题标题】:Spring Boot + Swagger + Swagger UI and @RequestBody has data type StringSpring Boot + Swagger + Swagger UI 和 @RequestBody 具有数据类型 String
【发布时间】:2016-12-11 02:14:17
【问题描述】:

我在使用 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

也许有人可以帮忙。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-04
    • 2017-06-29
    • 2018-01-27
    • 2021-07-20
    • 2020-04-11
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多