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

  • Home
  • SEARCH
  • 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 8219705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:16:13+00:00 2026-06-07T13:16:13+00:00

To solve another problem I have moved from using Jersey to EclipseLink MOXy to

  • 0

To solve another problem I have moved from using Jersey to EclipseLink MOXy to generate JSON from a JAXB created object model ( created by Sun JAXB 2.1.12). One difference I’ve noticed is the formatting of the output

that Jersey outputs

{"artist-list":{"offset":0,"count":1,"artist":[{"score":"100","type":"Group","id":"4302e264-1cf0-4d1f-aca7-2a6f89e34b36","name":"Farming Incident","sort-name":"Incident, Farming","gender":"male","country":"AF","disambiguation":"the real one","ipi-list":{"ipi":["1001","1002"]},"life-span":{"begin":"1999-04","ended":"true"},"tag-list":{"tag":[{"count":5,"name":"thrash"},{"count":11,"name":"güth"}]}}]}}

but MOXy gives

"count" : "1",
   "offset" : "0",
   "artist" : [ {
      "id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36",
      "type" : "Group",
      "score" : "100",
      "name" : "Farming Incident",
      "sort-name" : "Incident, Farming",
      "gender" : "male",
      "country" : "AF",
      "disambiguation" : "the real one",
      "ipis" : [ "1001", "1002" ],
      "life-span" : {
         "begin" : "1999-04",
         "ended" : "true"
      },
      "tags" : [ {
         "count" : "5",
         "name" : "thrash"
      }, {
         "count" : "11",
         "name" : "güth"
      } ]
   } ]
}

Moxy is much prettier 🙂
But one of the reasons to move to make our data available via Json is to reduce transmission bandwidth so is it possible to get MOXy to generate all one line, and without the extra spaces around each : ?

  • 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-07T13:16:14+00:00Added an answer on June 7, 2026 at 1:16 pm

    By default EclipseLink JAXB (MOXy) will marshal the JSON to one line. To get formatted output you need to set the following property on the Marshaller:

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    

    Root

    package forum11450509;
    
    public class Root {
    
        private String foo;
        private int bar;
    
        public String getFoo() {
            return foo;
        }
    
        public void setFoo(String foo) {
            this.foo = foo;
        }
    
        public int getBar() {
            return bar;
        }
    
        public void setBar(int bar) {
            this.bar = bar;
        }
    
    }
    

    jaxb.properties

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    The following code demonstrates how to specify formatted output:

    package forum11450509;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(2);
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);
    
            Root root = new Root();
            root.setFoo("ABC");
            root.setBar(123);
    
            System.out.println("One Line:");
            Marshaller marshaller = jc.createMarshaller();
            marshaller.marshal(root, System.out);
    
            System.out.println("\nFormatted:");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    Output

    Below is the output from running the demo code:

    One Line
    {"bar":123,"foo":"ABC"}
    Formatted:
    {
       "bar" : 123,
       "foo" : "ABC"
    }
    

    JAX-RS

    The same behaviour holds for MOXyJsonProvider (see: http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html).

    One Line

    By default when you include MOXyJsonProvider in your JAX-RS application, the output will be marshalled on one line:

    package org.example;
    
    import java.util.*;
    import javax.ws.rs.core.Application;
    import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
    
    public class CustomerApplication  extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            HashSet<Class<?>> set = new HashSet<Class<?>>(2);
            set.add(MOXyJsonProvider.class);
            set.add(CustomerService.class);
            return set;
        }
    
    }
    

    Formatted Output

    You can also configure MOXyJsonProvider to produce formatted output:

    package org.example;
    
    import java.util.*;
    import javax.ws.rs.core.Application;
    import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
    
    public class CustomerApplication  extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            HashSet<Class<?>> set = new HashSet<Class<?>>(1);
            set.add(ExampleService.class);
            return set;
        }
    
        @Override
        public Set<Object> getSingletons() {
            MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
            moxyJsonProvider.setFormattedOutput(true);
        }
    
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another problem that I can't solve I have a following code that
I have a simple, real life problem I want to solve using an OO
Here's another one for you to help me solve: I have an ASP.NET website
I have a problem using DatagramSocket. The problem is that I can't run two
I have 3 jugs of water problem to solve but with a little trick.I
I have an optimizer that solves a transportation problem, using a cost matrix of
I seem to have a strange problem: I coded an application in C++ (using
I have following code: VAR1= ANOTHER_VAR=$VAR1/path/to/file ANOTHER_VAR_2=$VAR1/path/to/another/file ... # getopts which reads params from
To solve some problem I need to compute a variant of the pascal's triangle
I tried to solve problems from Project Euler. I know my method would work

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.