carlspring carlspring - 1 year ago 75
Java Question

Jersey XML indentation

I have the following code:

public class SearchRestlet
extends BaseRestlet

@Produces({ MediaType.APPLICATION_XML,
MediaType.TEXT_PLAIN })
public SearchResult search(...)
throws IOException, ParseException
final SearchRequest searchRequest = new SearchRequest(...);
final SearchResult result =;

return result;


Here's my
mapped object:

@XmlRootElement(name = "result")
public class SearchResult

private String id;



This is the output I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><result><id>foo</id></result>

This is the output I'd like to get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

If I were using a marshaller directly, I know I can set a property for this and that it should work. How can this be done from within the context of Jersey based on the example above?

Answer Source

Add the following to your web.xml deployment descriptor:


For more details, check MessageProperties.XML_FORMAT_OUTPUT.

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