spring data jpa后端直接接收前端传过来的实体对象,前后端注意事项

后端controller代码,直接接收NcovPatientReport 对象:

@RequestMapping(value = "/savePatientReport", method = RequestMethod.PUT)
	@ApiOperation("患者报告信息保存")
	@ApiImplicitParams({})
	public ResultModel<NcovPatientReport> savePatientReport(NcovPatientReport report) {
		try {
			NcovPatientReport patientReport = nvocService.savePatientReport(report);
			return ResultModel.success(patientReport);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getLocalizedMessage(), e);
			return ResultModel.failure();
		}
	}

前端调用接口方式,注意data参数不要stringify:

//患者信息保存
        for (var patientItem in patientReport) {
            if (patientItem == "upDate"){
                debugger
            }
            patientReport[patientItem] = $(".patInfo").find("input[nameCode="+ patientItem +"]").val()
        }
        patientReport["sex"] = $("div[nameCode=sex] input:radio:checked").val();
        patientReport["ethnic"] = $("select[nameCode=ethnic] option:selected").val();
        patientReport["peopleCategoryCode"] = $("select[nameCode=peopleCategoryCode] option:selected").val();
        patientReport["clinicalType"] = $("select[nameCode=clinicalType] option:selected").val();
        patientReport["upContent"] = $("textarea[nameCode=upContent]").val();
        patientReport["diag"] = $("textarea[nameCode=diag]").val();
        $.ajax({ 
            url: "/ps/api/ncov/savePatientReport",
            dataType: "json",
            data:patientReport,
            type: "put",
            success: function (resp) {
                console.log(resp)
            }
        })

前端效果截图:
在这里插入图片描述
前端调用时加入遇到400错误,看看preview里的错误消息:
在这里插入图片描述
错误是由于前端日期格式和后端日期格式不统一导致的:
在这里插入图片描述

此时,在后端实体类中增加注解@DateTimeFormat(pattern = “yyyy-MM-dd”),实体类日期格式截图:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章