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 8383655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:12:54+00:00 2026-06-09T17:12:54+00:00

I’m trying to use MOXy to generate a specifically formatted response for a front

  • 0

I’m trying to use MOXy to generate a specifically formatted response for a front end client to make it easier for them to determine specific relationships. This is a bit of a contrived example, but I’m trying to do something like the following…

public class Person {
    private Integer id;

    private String name;

    private Address address;
}

public class Address {
    private String street;

    private String locality;

    private String state;

    private String zip;
}

public class PersonCollectionResponse {
    private Set<Person> people;

    public Set<Person> getPeople() {
        return this.people;
    }

    private int getCount() {
        return this.people.size();
    }
}

…and would like to end up with a result like…

{
    "meta": {
        "count": 2
    },

    "people": [{
        "id": 1,
        "name": "Danny Parker",
        "contact": {
            "locality": "Zoo York",
            "state": "New York"
        },
    }, {
        "id": 2,
        "name": "Oscar the Grouch",
        "contact": {
            "locality": "San Francisco",
            "state": "California"
        }

    }],

    "mappings": [{
        "person": 1,
        "zip": 10014
    }, {
        "person": 2,
        "zip": 94102
    }]
}

Now the “people” set isn’t very hard, but I’m stuck on how to get the mappings to work when writing the EclipseLink OXM metadata. I’m looking for a way to redefine how people get listed so that I can get my mapping.

<xml-element java-attribute="people">
    <xml-element java-attribute="id" xml-path="@id" />
    <xml-element java-attribute="zip" xml-path="@zip" />
</xml-element>

Not I get that might now be possible. So I created a getMappings on the PersonCollectionResponse that returns a List> that it generates from the people. Now this isn’t ideal, but there are worse things. But when it is written out, the map objects are strings like so…

"mappings": ["{person: 1, zip: 10014}", "{person: 2, zip: 94102}"]

So I guess my questions are one of these…

  1. How can I display an element a second time but with a different output?
  2. How can I output maps without them being quoted as strings?

Worst case scenario I guess is I make my set of generic maps into a new class objects and provide a schema for them too, but that is something I’d like to avoid.

  • 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-09T17:12:55+00:00Added an answer on June 9, 2026 at 5:12 pm

    Currently MOXy does not allow you to split the representation of a single instance into multiple locations within a JSON (or XML) tree. Please enter an enhancement request for the type of support you would like to see:

    • https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink

    Below I’ll demonstrate how to map portions of your JSON message.

    PARTIAL ANSWER #1

    Below I’ll describe how to map your model to the following JSON message:

    {
       "meta" : {
          "count" : 2
       },
       "people" : [ {
          "id" : 1,
          "name" : "Danny Parker",
          "contact" : {
             "locality" : "Zoo York",
             "state" : "New York"
          }
       }, {
          "id" : 2,
          "name" : "Oscar the Grouch",
          "contact" : {
             "locality" : "San Francisco",
             "state" : "California"
          }
       } ]
    }
    

    oxm.xml

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum11870107">
        <java-types>
            <java-type name="PersonCollectionResponse">
                <java-attributes>
                    <xml-element java-attribute="count" xml-path="meta/count/text()"/>
                </java-attributes>
            </java-type>
            <java-type name="Person">
                <xml-type prop-order="id name address"/>
                <java-attributes>
                    <xml-element java-attribute="address" name="contact"/>
                </java-attributes>
            </java-type>
            <java-type name="Address">
                <java-attributes>
                    <xml-transient java-attribute="zip"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    PARTIAL ANSWER #2

    Now I’ll describe how to map your model to the following JSON message:

    {
       "meta" : {
          "count" : 2
       },
       "mappings" : [ {
          "person" : 2,
          "zip" : "94102"
       }, {
          "person" : 1,
          "zip" : "10014"
       } ]
    }
    

    oxm.xml

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum11870107">
        <java-types>
            <java-type name="PersonCollectionResponse">
                <java-attributes>
                    <xml-element java-attribute="count" xml-path="meta/count/text()"/>
                    <xml-element java-attribute="people" name="mappings"/>
                </java-attributes>
            </java-type>
            <java-type name="Person">
                <xml-type prop-order="id address"/>
                <java-attributes>
                    <xml-element java-attribute="id" name="person"/>
                    <xml-transient java-attribute="name"/>
                    <xml-element java-attribute="address" xml-path="."/>
                </java-attributes>
            </java-type>
            <java-type name="Address" xml-accessor-type="NONE">
                <java-attributes>
                    <xml-element java-attribute="zip"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    DEMO CODE

    The same demo code can be used with both partial solutions:

    package forum11870107;
    
    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>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11870107/oxm.xml");
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {PersonCollectionResponse.class}, properties);
    
            PersonCollectionResponse pcr = new PersonCollectionResponse();
    
            Person person1 = new Person();
            person1.setId(1);
            person1.setName("Danny Parker");
            pcr.getPeople().add(person1);
            Address address1 = new Address();
            address1.setLocality("Zoo York");
            address1.setState("New York");
            address1.setZip("10014");
            person1.setAddress(address1);
    
            Person person2 = new Person();
            person2.setId(2);
            person2.setName("Oscar the Grouch");
            pcr.getPeople().add(person2);
            Address address2 = new Address();
            address2.setLocality("San Francisco");
            address2.setState("California");
            address2.setZip("94102");
            person2.setAddress(address2);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(pcr, System.out);
        }
    
    }
    

    DOMAIN MODEL

    Below is the domain model that was used for both partial solutions:

    PersonCollectionResponse

    package forum11870107;
    
    import java.util.*;
    
    public class PersonCollectionResponse {
        private Set<Person> people = new HashSet<Person>();
    
        public Set<Person> getPeople() {
            return this.people;
        }
    
        public void setPeople(Set<Person> people) {
            this.people = people;
        }
    
        public int getCount() {
            return this.people.size();
        }
    
    }
    

    Person

    package forum11870107;
    
    public class Person {
        private Integer id;
    
        private String name;
    
        private Address address;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
    }
    

    Address

    package forum11870107;
    
    public class Address {
    
        private String street;
        private String locality;
        private String state;
        private String zip;
    
        public String getStreet() {
            return street;
        }
    
        public void setStreet(String street) {
            this.street = street;
        }
    
        public String getLocality() {
            return locality;
        }
    
        public void setLocality(String locality) {
            this.locality = locality;
        }
    
        public String getState() {
            return state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public String getZip() {
            return zip;
        }
    
        public void setZip(String zip) {
            this.zip = zip;
        }
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html.

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.