简体   繁体   English

如何在 angular 应用程序上显示来自 springboot 的异常消息

[英]How to display exception message from springboot on angular application

I was working on spring boot + angular integration and I want to know how I can display exception message in my angular application.我正在研究 spring 启动 + angular 集成,我想知道如何在我的 angular 应用程序中显示异常消息。 I have created exception handler class in spring boot something like this:我在 spring 引导中创建了异常处理程序 class,如下所示:

@ControllerAdvice
public class ExceptionController {

@ExceptionHandler(value={CommonException.class})
public ResponseEntity<Object> noResult(CommonException e){
    HttpStatus status=HttpStatus.BAD_REQUEST;       
    return new ResponseEntity<Object>(new CustomExceptionPayload(e.getMessage(),"400"), status);
}
}

and in angular side I am sending request through service class using httpclient like this:在 angular 方面,我使用 httpclient 通过服务 class 发送请求,如下所示:

public generatFile(file:any,to:string,from:string,proj_name:string,count:string,env:string,event:string){
    console.log("in service");
    const formData=new FormData();
    formData.append('file',file);
    formData.append('to',to);
    formData.append('from',from);
    formData.append('proj_name',proj_name);
    formData.append('event',event);
    formData.append('env',env);
    formData.append('count',count);

    return this.http.post(this.baseUrl+'/events',formData,{
        observe: 'response',
        responseType: 'blob' as 'json'
      }
    );
}

And in my main component receiving response like this:在我的主要组件中接收这样的响应:

this.service.generatFile(this.file,new Date(this.to).toLocaleDateString(),new Date(this.from).toLocaleDateString(),this.proj_name,this.count,this.env,this.event).subscribe((resp: any) => {
    const blob = new Blob([resp.body], { type: resp.headers.get('Content-Type') });
    saveAs(blob, this.fileName);
    this.loading=false;
  },((error:any) =>{
    this.message=error.message;
    this.loading=false;
  }

I was using error.message in angular to get the error message as given above but it didn't work.我在 angular 中使用 error.message 来获取上面给出的错误消息,但它不起作用。

When i use postman i get this as response:当我使用 postman 时,我得到以下响应:

{
"message": "number of events per file is greater than number of failed events",
"status": "400"
}

I don't know how to get same error message in angular which I'm sending from spring boot.I will appreciate if anyone can provide the answer with code.我不知道如何在 angular 中获得相同的错误消息,这是我从 spring 引导发送的。如果有人可以提供代码答案,我将不胜感激。

You can use error object from observable.您可以使用 observable 中的错误object。

this.service.generatFile().subscribe(
    (res) => {
        // handle response
    }, (err) => {
        // handle errors
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 SpringBoot 中的异常消息中删除反斜杠? - How to remove backslash from exception message in SpringBoot? springboot项目中不显示异常错误信息 - Exception error message not display in springboot project 出现异常如何自动重启springboot应用 - how to automatically restart springboot application in case of exception Package Angular项目如何用SpringBoot应用 - How to Package Angular Project with SpringBoot application 如何在 Angular 应用程序中显示来自 resultSet 的数据 - How to display data from resultSet in Angular application springboot应用程序中如何全局处理数据库连接异常? - How to handle database connection exception globally in springboot application? 如何从springboot应用程序跟踪mongodb的变化 - How to track changes in mongodb from springboot application Spring WebFlux:如果抛出此异常,如何在网页上显示来自自定义异常类的消息? - Spring WebFlux: how to display a message from custom exception class on a Web page in case this exception is thrown? 如何在使用 angular7 和 springboot 构建的 Web 应用程序中使浏览器缓存无效 - How to invalidate browser cache in a web application built with angular7 and springboot Springboot应用程序在角度上出现fsevents错误 - fsevents error on angular with springboot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM