swagger 檔案上傳以及requestbody引數傳遞
阿新 • • 發佈:2021-10-27
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"}) publicStringgetHttpInfo(HttpServletRequestrequest,IntegeruserId)throwsIOException{ InputStreamin=request.getInputStream(); StringrequestParams=IOUtils.toString(in); //TODO.LOGIC returnrequestParams; }
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") publicStringupdateAvatar(IntegeruserId,@RequestPart("file")MultipartFilefile)throwsException{ //TOTO.LOGIC. returnfile.getName(); }
參考資料