Ich benutze Jackson Mapper Version 2.6.5 mit Spring Boot, aber ich kann scheinen SerializationFeature.INDENT_OUTPUT
zu arbeiten. Ich folge dem Tutorial here. Mein Code ist wie folgt.SerializationFeature.INDENT_OUTPUT ignoriert auf Jackson 2.6.5
public class SerializationExampleTreeModel {
public static void main(String[] args) throws IOException {
// Create the node factory that gives us nodes.
JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
StringWriter stringWriter = new StringWriter();
// create a json factory to write the treenode as json. for the example
// we just write to console
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator generator = jsonFactory.createGenerator(stringWriter);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
// the root node - album
JsonNode album = nodeFactory.objectNode();
album.put("Album-Title", "Kind Of Blue")
ArrayNode songs = nodeFactory.arrayNode()
songs.add("Song8").add("Song2")
album.put("Songs", songs)
ObjectNode artist = nodeFactory.objectNode()
artist.put("Name", "Alex")
album.put("artist", artist)
mapper.writeTree(generator, album)
println stringWriter.toString()
}
}
habe ich immer das Ergebnis:
{"Album-Title":"Kind Of Blue","Songs":["Song8","Song2"],"artist":{"Name":"Alex"}}
ob ich die Linie mapper.configure(SerializationFeature.INDENT_OUTPUT, true)
oder nicht enthalten. Was ist los?
Hinweis: Ich kompiliere meinen Code mit groovyc
und Semikolons sind nicht erforderlich.