Abhishek Mukhopadhyay Abhishek Mukhopadhyay -4 years ago 169
JSON Question

Jackson JSON pretty print using annotaions

I'm using

to convert a response bean to JSON in Lagom Framework. But, the JSON is not formatted. How can I pretty print the JSON using the annotations (not ObjectMapper)? Here's my sample response bean:

public class foo {

private final List<Result> _result;

private final MetadataBean _meta;

public foo (List<Result> _result, MetadataBean _meta) {
this._result= _result;
this._meta = _meta;


Answer Source

It seems that pretty printing is controlled by the ObjectMapper and cannot be influenced by annotations. The Lagom documentation for negotiated serializers has this example:

public class JsonTextSerializer implements MessageSerializer.NegotiatedSerializer<String, ByteString> {
    private final ObjectMapper mapper = new ObjectMapper();

    public MessageProtocol protocol() {
        return new MessageProtocol(Optional.of("application/json"), Optional.empty(), Optional.empty());

    public ByteString serialize(String s) throws SerializationException {
        try {
            return ByteString.fromArray(mapper.writeValueAsBytes(s));
        } catch (JsonProcessingException e) {
            throw new SerializationException(e);

Pretty printing can then be enabled on the mapper (probably in a constructor):

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download