swagger 文件上传以及requestbody参数传递

摘要: 记录swagger 文件上传,以及swagger requestbody方式API测试接受数据的配置方式。

swagger用来做普通的API测试很方便,在这一篇文章中讲过: http://www.yihaomen.com/article/1831.html, 但在实际开发过程中,经常会有文件上传,或者通过reuestbody传递数据等方式. 这个时候swagger的配置就有一些特殊了

swagger requestbody的配置方式

@ApiOperation(value = "测试requestBody", notes = "测试requestBody")
@ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name="userId", value="用户id", dataTypeClass = Integer.class, required = true),
        @ApiImplicitParam(paramType="body", name = "body",dataType = "string", example = "", required = false)
})
@PostMapping(value="/command",produces = {"application/json;charset=UTF-8"})
public String getHttpInfo(HttpServletRequest request, Integer userId) throws IOException {
    InputStream in = request.getInputStream();
    String requestParams = IOUtils.toString(in);
    // TODO. LOGIC
    return requestParams;
}

swagger 文件上传配置方式

@ApiOperation(value = "测试swagger3上传", notes = "测试swagger3上传")
@ApiImplicitParams({
        @ApiImplicitParam(name = "file", paramType="form", value = "临时文件", dataType="file", required = true),
        @ApiImplicitParam(name="userId", value="用户ID", dataTypeClass = Integer.class, required = true)
})
@PostMapping(value = "/update_avatar")
public String updateAvatar(Integer userId, @RequestPart("file") MultipartFile file) throws Exception {
    // TOTO. LOGIC.
    return file.getName();
}

参考资料

springboot 与 swagger3集成

上一篇: java POI,easyexcel 导入导出文档总结
下一篇: VUE自定义折叠面板组件-自己造轮子
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号