[英]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.