swagger 文件上傳以及requestbody參數傳遞


swagger用來做普通的API測試很方便,在實際開發過程中,經常會有文件上傳,或者通過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();
}

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM