Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this tutorial, we want to have a look at how to return HTML from a Spring MVC controller.

Let's take a look at what needs to be done.

2. Maven Dependency

First, we have to add the spring-boot-starter-web Maven dependency for our MVC controller:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <versionId>1.3.7.RELEASE</versionId>
</dependency>

3. Controller

Next, let's create our controller:

@Controller
public class HtmlController {
    @GetMapping(value = "/welcome", produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody
    public String welcomeAsHTML() {
        return "<html>\n" + "<header><title>Welcome</title></header>\n" +
          "<body>\n" + "Hello world\n" + "</body>\n" + "</html>";
    }
}

We use the @Controller annotation to tell the DispatcherServlet that this class handles HTTP Requests.

Next, we configure our @GetMapping annotation to produce MediaType.TEXT_HTML_VALUE output.

And finally, the @ResponseBody annotation tells the controller that the object returned should be automatically serialized to the configured media type, that is, TEXT_HTML_VALUE, or text/html.

Without this last annotation, we'd receive a 404 error since a String return value by default refers to a view name.

With that controller in place, we can test it out:

curl -v localhost:8081/welcome

The output will look similar to:

> ... request ...
>
< HTTP/1.1 200
< Content-Type: text/html;charset=UTF-8
< ... other response headers ...
<

<html>
<header><title>Welcome</title></header>
<body>
Hello world
</body>
</html>

As expected, we see that the Content-Type of the response is text/html. Furthermore, we see that the response also has the correct HTML content.

4. Conclusion

In this article, we looked at how to return HTML from a Spring MVC controller.

As always, code samples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!