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
Sep 2021, 28.

 

My next events:
🗓 Oct 05 17:00 Jumpstart your Java applications with AWS Lambda and Micronaut