java 后端接收数据类型整理(get、post)
直接实体接收
@PostMapping("/create")
public JSONResult create(@RequestBody CustomCreateRequest createRequest) throws Exception {
return JSONResult.OK(customService.create(createRequest));
}
String 接收后再转实体
@PostMapping("/create")
public JSONResult Create(@RequestParam String param) {
CustomCreateRequest createRequest = JsonUtils.getRestApiJsonMapper().fromJson(param, CustomCreateRequest.class);
return JSONResult.OK(customService.createByErp(createRequest));
}
直接接收某一参数
@PostMapping("/delete_enclosure")
public JSONResult deleteMeetingEnclosure(@RequestParam("param") String paramJson,@RequestParam("type") Integer type) {
LOGGER.info("delete|enclosure|param|{}", paramJson);
erpMeetingService.deleteMeetingEnclosure(paramJson);
return JSONResult.OK();
}
以request接收后转换成map 数字类型的转换需要前端传String类型 否则默认为double 类型导致失败
@PostMapping("export__info")
public JsonResult accountExport(HttpServletRequest request) throws Exception {
Map requestMap = new Gson().fromJson(request.getReader(), Map.class);
List<String> userIds = requestMap.get("user_ids") != null ? (List) requestMap.get("user_ids") : new ArrayList();
String projectNo = (String) requestMap.get("project_no");
Integer approvalStatus = Objects.equals(requestMap.get("status"), "") ? null :
Integer.valueOf((String) requestMap.get("status"));
}
map形式接收
@PostMapping("change_orders")
public JSONResult changeOrders(@RequestBody Map<String, Object> params) {
List<Long> ids = ((List<Integer>) params.get("ids")).stream().map(Long::new).collect(Collectors.toList());
return JSONResult.OK();
}
接收附件带表单的
@ResponseBody
@ApiOperation(value = "保信息处理", notes = "")
@PostMapping(value = "saveHandleInfo")
public ResponseData saveHandleInfo(HttpServletRequest request, @RequestParam("body") String json) throws ParseException {
User user = UserService.getUserInfo();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("file");
ComplaintHandleDto complaintHandleDto = JSON.parseObject(json, ComplaintHandleDto.class);
iComplaintHandleService.saveComplaintHandleInfo(complaintHandleDto, files);
return new SuccessResponseData();
}
直接收附件的
@ResponseBody
@ApiOperation(value = "上传信息", notes = "")
@PostMapping(value = "submit")
public ResponseData submit(HttpServletRequest request) throws IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("file");
return new SuccessResponseData(iComplaintHandleService.getComplaintInfoInit(files.get(0)));
}
get
对于get 请求稍微特殊的接收list 或者数组形式
@GetMapping("list_data")
public JsonResult listPageData(int start, int length,
@RequestParam(value = "user_ids", required = false) List<String> userIds) {}
前端传递方式为 user_ids=2,3,4 这样也可接收