Search code examples
javajsonjacksonannotationsjackson2

Is it possible to see the JSON created by Jackson Annotations?


I'm using https://github.com/FasterXML/jackson-annotations in a Spring 5 project. I am facing some issues sending data to a server, because somewhat one of the fields of the object that is serialized is not in the correct format. Is it possible to see the JSON created by Jackson Annotations? That would make much easier for me to see what is wrong with the JSON representation that is being sent to the API I need to consume.


Solution

  • You could try with this -

    ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); 
    try { 
        String json = mapper.writeValueAsString(<your-class-here>);
        System.out.println(json); 
    } catch (JsonProcessingException e) { 
        e.printStackTrace(); 
    }
    

    Just replace "your-class-here" with your generated object reference.