简体   繁体   中英

How to send html content with xml/json request in rest api?

I am writing a restful service to send the email in spring boot. API request is mapped with object.
When I am sending xml in request it works fine but in body I want to send HTML content.

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>

With 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);

....
}

When I add HTML in XML it fails to map request with the object.

as you are sending html data, xml will consider each tag as its own, so to avoid this, try sending html data in another tag of xml with double quotes like: "Page Titlebody { background-color: lack; text-align: center; color: white;}

This is a Heading

This is a paragraph.

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM