我正在使用angularjs和Spring Mcv Rest开发一个单页应用程序。 我正在像Angularjs中那样调用我的服务(使用javax邮件发送邮件):SendProformaFax.get({idCommande:$scope.commande.id})
SendProformaFax.get({idCommande:$scope.commande.id})
在服务器端,我的服务是:
@RequestMapping(value = "/sendProformaFax/{idCommande}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public void imprimeProforma(@PathVariable String idCommande) { Commande commande = commandeRepository.findOne(new Long(idCommande)); List<Vente> ventes = venteRepository.findAllByCommande(commande); blService.sendProformaFax(ventes); }
当函数sendProformaFax抛出MessagingException时,我想显示一条消息。
我不知道如何在我的RestController中返回此异常以及如何在Angularjs中捕获它。
如果有人可以帮助我… 谢谢。
编辑:在服务器端我正在这样做:
@ExceptionHandler(value = Exception.class) public ErrorView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it - like the OrderNotFoundException example // at the start of this post. // AnnotationUtils is a Spring Framework utility class. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e; // Otherwise setup and send the user to a default error-view. ErrorView mav = new ErrorView(); mav.setException(e.getMessage()); mav.setUrl(req.getRequestURL().toString()); mav.setMessage("Veuillez contacter le support informatique."); return mav; }
在Angularjs方面,我正在这样做
CreateFichierCiel.get({params:param}, function (response) { $scope.infoMessage = "La génération du fichier CIEL est terminée." $activityIndicator.stopAnimating(); $("#messageModal").modal('show'); $scope.find(); }, function (reason) { $("#errorModal").modal('show'); })
但是“原因”对象是这样的:
配置:对象数据:对象错误:“内部服务器错误”异常:“ java.lang.NullPointerException”消息:“无可用消息”路径:“ / api / createFichierCiel / 15-00005”状态:500时间戳:1438430232307原型:对象标头:函数(名称){状态:500 statusText:“内部服务器错误”原型:对象
因此,我没有从服务器发送ErrorView类。如果有人在这里看到我错了…
谢谢
您可以ExceptionHandler为MessagingException和设置HTTPStatus,以指示响应有错误(egz。 BAD_REQUEST)
ExceptionHandler
MessagingException
HTTPStatus
BAD_REQUEST
@ExceptionHandler(MessagingException.class) @ResponseStatus(HTTPStatus.BAD_REQUEST) @ResponseBody public ErrorView handleMessagingException(MessagingException ex) { // do something with exception and return view }
在AngularJS中,您可以像这样从资源服务中捕获它:
MessagingService.get({idCommande: 1}, function (data) { // this is success }, function (reason) { // this is failure, you can check if this is a BAD_REQUEST and parse response from exception handler };
使用时几乎相同$http。
$http