Ebooks

Spring Boot serve text

Spring Boot serve text tutorial shows how to serve plain text in a Spring Boot application.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.

Content type

Content type, or media type, is a string sent along with a file indicating the type of the file. It describes the content format; for example, an HTML file might be labeled text/html, or an image file as image/png). It serves the same purpose as filename extensions on Windows.

The content-type header values is used to indicate the media type of the resource. The text/plain; charset=utf-8 is used for text files.

Spring Boot serve text example

The following application shows three ways to send text to the client.

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
└───test
    └───java

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>SpringBooSendText</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <dependencies>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is the Maven pom.xml file. We only need the spring-boot-starter-web dependency.

com/zetcode/controller/MyController.java
package com.zetcode.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;


@Controller
public class MyController {

    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody
    String home() {

        return "home page";
    }

    @GetMapping(value = "/about")
    public void test(HttpServletResponse response) throws IOException {

        response.addHeader("content-type", "text/plain; charset=utf-8");
        response.setStatus(200);

        PrintWriter out = response.getWriter();
        out.println("about page");
    }

    @GetMapping(value = "/contact")
    public ResponseEntity<String> contact() {

        var httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));

        return new ResponseEntity<>("contact page", httpHeaders, HttpStatus.OK);
    }
}

We have three methods to return text in the controller. Each method uses a different technique.

@Controller
public class MyController {

    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody
    String home() {

        return "home page";
    }

Since the controller is annotated with the @Controller annotation, we have to add the @ResponseBody annotation to directly write to the body of the response rather that returning a view name to be processed. The home() method has a String return type and the produces attribute is set to MediaType.TEXT_PLAIN_VALUE.

@GetMapping(value = "/about")
public void test(HttpServletResponse response) throws IOException {

    response.addHeader("content-type", "text/plain; charset=utf-8");
    response.setStatus(200);

    PrintWriter out = response.getWriter();
    out.println("about page");
}

The second way uses the HttpServletResponse. It is a low-level approach where we directly write to the response object.

@GetMapping(value = "/contact")
public ResponseEntity<String> contact() {

    var httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));

    return new ResponseEntity<>("contact page", httpHeaders, HttpStatus.OK);
}

In the third case, we use the ResponseEntity to serve text. The media type is set in the HttpHeaders.

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application  {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Application is the entry point which sets up Spring Boot application.

We run the application with mvn -q spring-boot:run.

$ curl localhost:8080/
home page
$ curl localhost:8080/about
about page
$ curl localhost:8080/contact
contact page

This is the output for all three pages.

$ curl -i localhost:8080/contact
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 12
Date: Tue, 14 Jan 2020 12:22:28 GMT

contact page

With the curl's -i option we also include the headers.

In this tutorial, we have shown how to send text data to the client from a Spring Boot application.

List all Spring Boot tutorials.