Spring Web MVC Cheat Sheet
Spring Web MVC Cheat Sheet
01
How can I return JSON payload
from a Spring MVC endpoint?
02
How to return a different HTTP
status for a Spring MVC controller?
04
How can I return a Thymeleaf
view from a controller?
05
How can I return XML from
my Spring MVC controller?
06
How can I provide different
content types for a controller?
07
How can I up-/download a file with
Spring Web MVC?
01
How can I return JSON payload from a Spring MVC endpoint?
@RestController
@RequestMapping("/json")
return orders;
);
return ResponseEntity.status(HttpStatus.I_AM_A_TEAPOT).body(orders);
02
Source Code | Blog Post
How can I validate the incoming payload to my controller?
@Validated
@RestController
@RequestMapping("/validation")
@GetMapping("/{message}")
}
The example above will validate that the path variable has between 5 and 10 characters and that
the query parameter size is a positive number. Sending an invalid request to this endpoint, like:
curl -v http://localhost:8080/validation/foo?size=-42
results by default in an HTTP status 500. If you want to change this behavior, you can add
an ExceptionHandler to catch the ConstraintViolationException and return HTTP status 400 (Bad
Request) instead:
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
03
Source Code | Blog Post
Either add this to your controller class or to a class annotated with @ControllerAdvice
Validating an incoming HTTP request body payload works a little bit differently. First, ensure you
use the Bean Validation annotations on the POJO that maps to the incoming request body:
@NotBlank
@Future
@PostMappingpublic
Sending an payload will result in an HTTP 400 response including information about validation
errors:
-d '{"message":"Hello","email":"notAnEmail","memberSince": "2025-04-20T12:00"}' \
http://localhost:8080/validation
04
03
Source Code | Blog Post
How can I return a Thymeleaf view from a controller?
To start using Thymeleaf, add the following Spring Boot Starter to your project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Next, you need an endpoint to return your Thyemleaf view. For this use @Controller to annotate
your controller class (hint: you can’t use @RestController here, as this returns the payload as part of
the HTTP body). There are multiple ways to tell Spring which view name to render. The simplest is to
return the name of the view as a String:
@Controller
@RequestMapping("/welcome")
@GetMapping
<!DOCTYPE HTML>
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<span th:text="${message}"></span>
</div>
</body>
</html>
05
03
Source Code | Blog Post
How can I return XML from my Spring MVC controller?
To return XML from your controller, you need an additional dependency from Jackson:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
If you are using the Spring Boot Starter Web, Spring Boot ensure to auto-configure everything to
make use of the JacksonXmlModule. What’s left is to annotate your POJO with @XmlRootElement
@XmlRootElement
Now you can return XML from your Spring MVC controller methods:
);
return orders;
06
03
Source Code | Blog Post
How can I provide different content types for a controller?
To be able to return different content types and let the client decide which type he can process
(content negotiation), pass multiple MediaTypedefinitions to the produces field of your mapping
annotation. This might be @GetMapping,@RequestMapping, @PostMapping, etc:
@GetMapping(path="/orders",
produces={MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
);
return orders;
A client can now decide which content type he wants to process using the HTTP Accept header:
[{"id":"42L","tags":["books","foreign"],"customerId":1,"orderAt":"2020-04
06T05:45:39.40289"},
{"id":"58B","tags":["computer","fractile"],"customerId":48,"orderAt":"2020-04
06T05:45:39.402908"}]
<List><item><id>42L</id><tags><tags>books</tags><tags>foreign</tags></tags>
<customerId>1</customerId><orderAt>2020-04-06T05:48:08.077563</orderAt></item>
<item><id>58B</id><tags><tags>computer</tags><tags>fractile</tags></tags>
<customerId>48</customerId><orderAt>2020-04-06T05:48:08.077587</orderAt></item>
</List>
07
03
Source Code | Blog Post
How can I up-/download a file with Spring Web MVC?
To upload a file to a Spring Web MVC controller endpoint, you can make use of Spring’s MultipartFile
class. A common approach to upload files is using an HTML form and the content type
multipart/form-data
@PostMapping
public void upload(@RequestParam("file") MultipartFile file) throws IOException {
For an example on how to download a file, let’s use a file available on the classpath (part of
src/main/resources). To instruct a client e.g. a browser that the response is a file, you have to set
some HTTP headers.
Using Content-Type you help a client to understand which kind of file is downloaded to e.g. suggest
opening the file with a PDF viewer. Furthermore, with Content-Length and Content-Disposition you
can add metadata like the filename and size to the response:
@GetMapping
.getResourceAsStream("/document.pdf").readAllBytes();
header.setContentType(MediaType.APPLICATION_PDF);
header.setContentLength(pdfContent.length);
08
03
Source Code | Blog Post