我试图处理我的角度的异常,该异常使用REST API将数据放入后端。问题是,尽管我为异常设置了适当的状态代码,但是只要有异常,REST中的响应实体就会将响应发送为null。我认为我的追赶阻拦没有被召唤。下面是从组件到服务的代码。
Schedule.component.ts
create(schedule: Schedule): void {
this.service.create(schedule).subscribe(data => {
if (data.created == true) {
this.gotoSchedules(schedule);
}
},
errorCode => {
this.statusCode = errorCode;
console.error(errorCode);
}
);//subscribe ends
}
schedule.service.ts
create(schedule: Schedule) : Observable<any> {
let params:URLSearchParams = this.scheduleParams(schedule);
let theUrl = window.location.origin + "/o/dsl-rest/dsl/schedule";
let options = new RequestOptions({responseType: ResponseContentType.Json});
return this.http.post(theUrl, params, options).map(res => res.json()).catch(this.handleError);
}
ScheduleController.java
@RequestMapping(value = "/schedule", method = RequestMethod.POST)
public ResponseEntity<String> createSchedule(@Context HttpServletRequest request, @Context HttpServletResponse response,
@FormParam("name") String name,
@FormParam("days") Integer[] days,
@FormParam("times") String[] times,
@FormParam("inside") String[] inside,
@FormParam("outside") String[] outside,
@FormParam("screenName") String screenName,
@FormParam("timeZone") String timeZone,
@FormParam("description") String description,
@FormParam("notifyEmails") String[] notifyEmails,
@FormParam("notifyInitial") Boolean notifyInitial,
@FormParam("notifyLate") Boolean notifyLate) throws NamingException
{
ResponseEntity<String> responseEntity = null;
long t1 = System.currentTimeMillis();
Map<String,Object> schedule = createScheduleMap(name, days, times, inside, outside,
screenName, timeZone, description, notifyEmails, notifyInitial, notifyLate);
try {
scheduleService.createSchedule(schedule);
responseEntity = new ResponseEntity<String>("{\"created\" : true}", HttpStatus.OK);
} catch(Exception e){
responseEntity = new ResponseEntity<String>("{\"status\" : error}", HttpStatus.INTERNAL_SERVER_ERROR);
}
finally {
long t2 = System.currentTimeMillis();
logger.debug("<< createSchedule() in " + (t2 - t1) + "ms");
}
return responseEntity;
}