我正在编写一个安静的服务来在 Spring Boot 中发送电子邮件。
API请求与对象映射。
当我在请求中发送 xml 时,它工作正常,但在正文中我想发送 HTML 内容。
Below is the request without html
<mailContent>
<from>abc@gmail.com</from>
<to>
<email>xyz@gmail.com</email>
</to>
<subject> MY ALERT </subject>
<contentType>multipart/mixed</contentType>
<body>
Hi Raj \n
How are you?
</body>
</mailContent>
使用 HTML
<mailContent>
<from>abc@gmail.com</from>
<to>
<email>xyz@gmail.com</email>
</to>
<subject> MY ALERT </subject>
<contentType>multipart/mixed</contentType>
<body>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body {
background-color: black;
text-align: center;
color: white;
}
</style>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<img src="https://www.w3schools.com/tryit/avatar.png" alt="Avatar" style="width:200px">
</body>
</html>
</body>
</mailContent>
Controller
public SendMailResult sendMail(@ApiParam("Contains the mail content and configurations to be used for sending mail") @Valid @RequestBody MailMessage message, BindingResult result) throws InterruptedException {
SendMailResult results = new SendMailResult();
message = sendemailService.prepareMessage(message);
....
}
当我在 XML 中添加 HTML 时,它无法将请求与对象映射。
最佳答案
当您发送 html 数据时,xml 会将每个标签视为自己的标签,因此为了避免这种情况,请尝试在 xml 的另一个标签中使用双引号发送 html 数据,例如: “页面标题正文{背景颜色:缺少;文本对齐:居中;颜色:白色;}
这是一个标题
这是一个段落。
”关于java - 如何在rest api中使用xml/json请求发送html内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57579657/