我在Angular 7中无法识别post方法的问题。我遇到了错误:Http failure response for http://localhost:4200/sendMail: 404 Not Found
。
这是我在Angular中的post方法:
sendEmail(email: EmailModel): Observable<EmailModel> {
const url = `sendMail`;
return this.http.post<EmailModel>(url, email, httpOptions).pipe(
tap(_ => this.log('Category is stored!!!')),
catchError(this.handleError<EmailModel>('addHero'))
)
}
这是我在Spring Boot中的post方法:
@PostMapping("/sendMail")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String sendMail(@RequestBody EmailModel emailModel) {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
System.out.println(emailModel);
try {
helper.setTo("saomi@gmail.com");
helper.setText("Greetings :)");
helper.setSubject("Mail From Spring Boot");
} catch (MessagingException e) {
e.printStackTrace();
return "Error while sending mail ..";
}
sender.send(message);
return "Mail Sent Success!";
}
}
当我通过邮递员在此路径上发送邮件时,在Spring Boot上可以很好地工作:
http://localhost:8080/sendMail
这是我的proxy.conf.json:
{
"/api": {
"target": "http://localhost:8080",
"secure": false,
"changeOrigin": true
}
}
其余API正常工作。