Search code examples
javajsonxmlxmltype

How to Covert a @XmlType to Json string?


I want to convert a an entity which is @XmlType to a Json string in java. I searched for such question but all I have found is to convert a XML string to Json string .


Solution

  • I would recommend to use the Jackson library for this job. This works with any Java class as long it's structure is somehow compatible to JSON:

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = ow.writeValueAsString(object);
    

    The "withDefaultPrettyPrinter" part is optional.