@RequestParam
- 最常用
- 用value指定参数名字,required字段指定参数是否必须,默认为true,当required=false时,一般配合着defaultValue="xx"使用
- 对应的url是这样的:
https://localhost/requestParam/test?key1=value1&key2=value2
@RequestMapping("/getXxx")@ResponseBodypublic String getXxx(HttpServletRequest request,@RequestParam(value="compareId", required = false)String compareId,@RequestParam(value="networkType", required = false)String networkType,@RequestParam(value="simulateDate", required = false)String simulateDate,@RequestParam(value="productType", required = false)String productType,@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){}
@PathVirable
- url是RestFul风格时,就无法用@RequestParam了,就用这个
- 对应的url是这个样子的
https://localhost/pathVariable/test/value1/value2
- 用value指定参数名字,required字段指定参数是否必须,默认为true
@RequestMapping("/pathVariable/test/{param}")@ResponseBodypublic String pathVariableTest(@PathVariable(value = "param",required = false)String param){return "pathVariable接受到的参数:" + param;}
@RequestBody
- 使用@RequestBody注解表示请求参数来自于请求体, 前台请求参数需以JSON格式传递
- 对应的url
- @RequestParam注解用于从前端传递过来的请求参数中获取单个或多个参数,一般用于GET请求,表示前端向后端索要数据。而@RequestBody注解则是从请求体中获取数据,一般用于POST请求,表示前端给后端提供数据
- @RequestBody注解接受到的JSON实体直接被转为java对象
@PostMapping("/realTimeExport")@Operation(summary = "实时查询车辆信息导出")public MessageBean<String> realTimeExport(@RequestBody CarInfoRealTimeQueryParamDTO carInfoRealTimeQueryParamDTO) {String export = carInfoService.export(2,null,carInfoRealTimeQueryParamDTO);return MessageBean.success(export);}
@ApiParam
使用swagger的时候才用得到这个注解,相当于swagger版本的@RequstParam
@RequestMapping(value="uploadFile",consumes = "multipart/*",headers = "content-type=multipart/form-data",method = RequestMethod.POST)@ResponseBodypublic DataResult uploadFile(@ApiParam(value = "上传文件",required = true) MultipartFile file, String resumeID,HttpServletRequest request)throws IOException{}}
@Parameter
这是swagger3提供的注解,基本上没人用。也是swagger版本的@RequstParam