Spring Web MVC Cheat Sheet

Last Updated:  November 29, 2021 | Published: April 6, 2020

This is a living document to provide a central place for common Spring Web MVC use cases. With this Spring MVC cheat sheet, you get a quick reference to solve re-occurring tasks for your endpoints like validation, content-negotiation, file-handling, etc.

If not stated differently, the examples assume your application uses the spring-boot-starter-web dependency and at least Java 9. Furthermore, you should at least use a Spring Boot version greater than 2.1. You can find the source code for these Spring Web MVC examples on GitHub.

Download the PDF version of this cheat sheet here.

How can I return JSON payload from a Spring MVC endpoint?

To return JSON payload from your Spring MVC endpoint, ensure your project is using the Spring Boot Starter Web. Using this Starter, Spring Boot ensures to auto-configure Jackson to serialize and deserialize Java objects to JSON.

Next, annotate your controller with @RestController to instruct Spring to bind the response of your method to the web response. Finally, specify the return type of your method using the produces attribute of your mapping (e.g. @GetMapping, @RequestMapping, etc.) annotation:

How to return a different HTTP status for a Spring MVC controller?

To return a different HTTP status from your controller, ensure your method returns the response wrapper ResponseEntity from Spring. You can specify the data type of the HTTP body as a type parameter, e.g. ResponseEntity<Order> to ensure type safety.

The ResponseEntity class then provides static methods to build the response according to your needs. This allows you to specify the HTTP status of the response manually:

How can I validate the incoming payload to my controller?

To validate incoming payload, you can make use of the Bean Validation API. If your project contains the Spring Boot Starter Web or WebFlux, you don't need any further dependencies as both depend on spring-boot-starter-validation.

Let's start with validating path variables ("/myPathVariable") and query parameters ("?q=duke&size=10"). For this, you need to annotate your controller with @Validated and add the Bean Validation annotations (coming from javax.validation or jakarta.validation) to your method arguments:

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:

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:

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:

Next, add @Valid to the @RequestBody annotation of your controller method:

Sending an payload will result in an HTTP 400 response including information about validation errors:

More information on validating payload can be found here:

How can I return a Thymeleaf view from a controller?

To start using Thymeleaf, add the following Spring Boot Starter to your project:

With this Starter, you'll get the correct Thyemleaf dependency and the autoconfiguration mechanism of Spring Boot ensures all required beans with the correct configuration are in place.

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:

You can inject the Model as a method argument and set any data you need to render your view.

By default convention, the Thymeleaf view resolver searches for templates at classpath:/templates/with the .html suffix. Therefore we can place our templates inside src/main/resources/templates:

The view is now accessible at http://localhost:8080/welcome and returns an HTML file including the message we set inside our controller.

More information on using Thymeleaf with Spring can be found here:

How can I return XML from my Spring MVC controller?

To return XML from your controller, you need an additional dependency from Jackson:

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 :

Now you can return XML from your Spring MVC controller methods:

How can I provide different content types for a controller?

First, make sure you have the corresponding HttpMessageConverter enabled. If you use the Spring Boot Starter Web, you'll get a converter for JSON configured, as the Jackson dependency is pulled with this starter. For XML, make sure to read the section above. Besides JSON and XML, Spring also provides a converter for String orbyte by default.

To now 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:

A client can now decide which content type he wants to process using the HTTP Accept header:

How can I upload 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.

Using this content type and file as a key, you can access your file in the backend as the following:

For a more full-stack example, consider reading my Up- and download files with React and Spring Boot blog post.

How can I download a file with Spring Web MVC?

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:

For a more full-stack example, consider reading my Up- and download files with React and Spring Boot blog post.

How can I use Spring Web MVC and WebFlux together?

If you plan to make use of the Spring WebFlux WebClient for making concurrent HTTP requests using the Reactor operators, simply include the spring-boot-starter-webflux to your project:

You can easily combine this with your existing Spring Web MVC application, as Spring Boot will still auto-configure the embedded Tomcat for you. Hence you can use your existing (blocking) Web MVC application and start using parts of WebFlux for requesting data from remote services.

Download the PDF version of this cheat sheet here.

Have fun using this Spring Web MVC cheat sheet,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>