2016-08-04 51 views
2

我有一個使用Spring Boot 1.4和Swagger和Swagger UI的問題。使用@RequestBody參數時顯示爲數據類型字符串。這似乎不正確。Spring Boot + 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

也許有人可以提供幫助。

回答

5

這似乎是Springfox中的一個錯誤(#1344)。你可以解決它通過不使用@ApiImplicitParams,但通過與@ApiParam註釋annoting你的方法本身的參數:

@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"); 
} 
+0

非常感謝!這工作!我已將解決方案推送到https://github.com/larmic/SpringBootAndSwaggerUI –