Micronaut Error Responses with Problem+JSON

problem+json is my preferred way of formatting API errors and it is easy to use it in a Micronaut Application.

A request to a non existing route:

curl -i locahost:8080/api/v1/bogus

returns:

{
    "message":"Not Found",
    "_links":{"self":{"href":"/api/v1/bogus","templated":false}},
    "_embedded":{"errors":[{"message":"Page Not Found"}]}
}

The previous payload is formatted with vnd.error. The response content type is application/json.

If you add the dependency io.micronaut.problem:micronaut-problem-json, the maven coordinate of Micronaut Problem JSON, the same request:

curl -i locahost:8080/api/v1/bogus

returns:

{
    "type":"about:blank",
    "status":404,
    "detail":"Page Not Found"
}

Moreover, the response content type is application/problem+json.

Micronaut Problem JSON helps you produce application/problem+json responses from a Micronaut application. It connects the Problem library and Micronaut Error Formatting capabilities.

Tags: #micronaut #problem #vnd