java - 如何在rest api中使用xml/json请求发送html内容?

标签 java spring rest spring-boot

我正在编写一个安静的服务来在 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/

相关文章:

java - 通过显式锁定/条件引发 IllegalMonitorStateException

java - Spring Boot 与 @PostConstruct 的集成测试取决于 @Before 部分

javascript - MongoDB中设计Shift schema的疑惑

rest - 使用 REST 获取多个资源(批量获取)的正确方法是什么?

Java REST api http方法类型来调用执行sql删除查询的存储过程

java - PHP-Dojo 与 Java 的大型应用程序对比

java - 从另一个类中获取值(value)

java - H2 内存 DB : Set Timezone with JDBC? Java 单元测试

java - 控制台maven编译错误

java - 分页Spring启动JPA异常:Method has to have one of the following return types