Spring Boot + Swagger + Swagger UI and @RequestBody has data type String

20,336

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:

@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");
}
Share:
20,336
Lars Michaelis
Author by

Lars Michaelis

Updated on July 09, 2022

Comments

  • Lars Michaelis
    Lars Michaelis almost 2 years

    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.

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

    I've created a full example here https://github.com/larmic/SpringBootAndSwaggerUI

    Maybe someone can help.