Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 935
Lars Michaelis Avatar asked Aug 04 '16 19:08

Lars Michaelis


People also ask

Can @RequestBody be a string?

The @RequestBody annotation allows us to retrieve the request's body. We can then return it as a String or deserialize it into a Plain Old Java Object (POJO). Spring has built-in mechanisms for deserializing JSON and XML objects into POJOs, which makes this task a lot easier as well.

What is the use of @RequestBody in spring boot?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.


1 Answers

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");
}
like image 91
g00glen00b Avatar answered Oct 19 '22 12:10

g00glen00b