post請求:http://localhost:9080/DataDiscoveryWeb/issueformcount/queryIssueTendencyDetail.xhtml?jobId=862
前端報500錯誤
1、500,是服務器的錯誤,查看一下后台,沒有報錯。
2、打斷點,也沒有發現錯誤,但是請求返回空數據的時候,沒有報錯,返回有數據的結果報錯了。
3、那應該是對象轉Json的時候報錯了,加入對象轉Json代碼到請求的最后。
ObjectMapper objectMapper = new ObjectMapper(); try { objectMapper.writeValue(System.out,output); } catch (IOException e) { e.printStackTrace(); }
4、再次測試,果然發現報錯了。ReportStatistics.getJobId()實體轉Json的時候空指針。
Caused by: java.lang.NullPointerException at com.audaque.datadiscovery.report.entity.ReportStatistics.getJobId(ReportStatistics.java:127) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483) at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418) at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) ... 59 more
5、查看代碼,getJobId的返回類型是int,查詢出的JobId是Null值,NUll不能轉為int,應該是將返回的返回類型改為Integer。實體類應該使用包裝類型,原因Java Bean 使用包裝類型 還是基本類型,我修改成包裝類型后,沒有重新生成get,set方法。
private Integer jobId; public int getJobId() { return jobId; }
前端代碼:
parent.postReturnJsonnoalert("/issueformcount/queryIssueTendencyDetail.xhtml",{ jobId:862 },function(result){ if(result.success){ debugger; parent.showInfoBox("查詢成功"); }else{ parent.showErrorBox(result.msg); } } );
后端代碼:
@RequestMapping(value = "queryIssueTendencyDetail.xhtml",method = RequestMethod.POST) @ResponseBody public EasyUIDataGradOutputModel queryIssueTendencyDetail ( Integer jobId) { EasyUIDataGradOutputModel output = new EasyUIDataGradOutputModel(); Page<ReportStatistics> page = null; try { //查詢100條數據 page = reportService.queryJobReportByJobId(jobId, 1, 100); } catch (AdqException e) { LOG.error(e.getMessage(),e); page = new Page<ReportStatistics>(); } output.setRows(page.getRecords()); output.setTotal((int) page.getTotalRows()); return output; }