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”),實體類日期格式截圖:
在這裏插入圖片描述

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