Take the 2-minute tour ×

Is there any plain way

  • to print a JAXB annotated instance
  • in JSON format (as exactly jersey will print in AS)
  • while running unit testing
  • without any embedded server launched?

I can run and see in XML with JAXBContext, Marshaller, and so on.

Is there any example for JSON?

I found it. It's similar to JAXB.

With Jersey API

final JSONJAXBContext context = new JSONJAXBContext(...);

final JSONMarshaller marshaller = context.createJSONMarshaller();

final JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();
final T unmarshalled = unmarshaller.unmarshalJAXBElementFromJSON(
        ..., T.class).getValue();

for Maven

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It is possible:

StringWriter writer = new StringWriter();
marshaller.marshallToJSON(objectToMarshall, writer)

You can use an StringWriter to take the String representation of the marshalled JSON output.

share|improve this answer

Your Answer


