Spring Boot

Handling YAML format in your REST with Spring Boot

Posted on by  
Willem Cheizoo

In general a REST service serves JSON document formats. In Spring Boot it is even the standard without having to configure anything. Over HTTP we have the ability to send a Content-Type as header to instruct the server to return a certain document format (Mime type). Besides handling JSON, XML is another common document format. But what if we want to serve or read a YAML document format? This tutorial provides the required steps to be able to handle objects in YAML format. To be able to send a YAML Content-Type with the header, and to be able to serialize and deserialize with the existing ObjectMappers of Jackson. This tutorial will use Spring Boot, Jackson and a YAML dataformat extension library for Jackson.

First we need to add the YAML Jackson extension to our Gradle build file.

Continue reading →

Spicy Spring: Customize error JSON response with ErrorAttributes

Posted on by  
Willem Cheizoo

The default JSON error body of Spring Boot can be customized or extended by defining our own ErrorAttributes implementation. In Spring Boot we get an error response JSON format for free. Any thrown Exception is automatically translated to this JSON format and returned with a corresponding HTTP status.

As soon as we throw an Exception in a @RequestMapping annotated method of a @Controller, the thrown Exception is translated to a HTTP status and a JSON error body. The default JSON error body looks like:

Continue reading →

shadow-left