Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:32:37+00:00 2026-06-01T22:32:37+00:00

I know about the possibility how to turn on the formatting when using Marshaller.

  • 0

I know about the possibility how to turn on the formatting when using Marshaller. But I’m using Apache CXF (JAX-RS) and returning a response like return Response.ok(entity).build();.

I haven’t found any option how to format the output. How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:32:39+00:00Added an answer on June 1, 2026 at 10:32 pm

    First off, the way to get formatted output of XML is to set the right property on the marshaller (typically JAXB when working with CXF, which is OK since JAXB does a creditable job). That is, somewhere you’re going to have something doing this:

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    

    The issue is that you don’t necessarily want to have all output formatted; it adds quite a bit to the overhead. Luckily, you’re already producing an explicit Response, so we can just use more features of that:

    Marshaller marshaller = JAXBContext.newInstance(entity.getClass()).createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter sw = new StringWriter();
    marshaller.marshal(entity, sw);
    
    return Response.ok(sw.toString(), MediaType.APPLICATION_XML_TYPE).build();
    

    Another method is mentioned in this JIRA issue (itself closed, but that’s not so much of an issue for you):

    The workaround is to register a custom output handler which can check whatever custom query is used to request the optional indentation:

    http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/FormatResponseHandler.java

    JAXBElementProvider and JSONProvider are driven by the JAXB Marshaller so by default they check a Marshaller.JAXB_FORMATTED_OUTPUT property on the current message.

    This leads to code like this:

    public class FormattedJAXBInterceptor extends AbstractPhaseInterceptor<Message> {
        public FormattedJAXBInterceptor() {
            super(Phase.PRE_STREAM);
        }
    
        public void handleMessage(Message message) {
            message.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }
    
        public void handleFault(Message message) {
            message.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }
    }
    

    The CXF site discusses registration of interceptors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know about specifying source locations using gdb. But I am wondering if there's
I know about map/reduce alghoritm and its use. It's using functions that are called
I know about the ADODB.Stream object . But what I really want is a
I know about Options->Fonts and Colors. But there is no option for F# strings(i
I know about $(Delphi) and $EDNAME but after much seaching I cant find a
I know about the boolean column type , but is there a boolean literal
I know about DATEDIFF(d, date1, date2), but I am not looking to subtract two
I have the following string: i:0#.w|domain\x123456 I know about the possibility to group searchterms
What is the best approach of using shell commands from vim? I know about
I've read several questions about testing android apps on device. I know possibility to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.