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(); }
參考資料