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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:49:52+00:00 2026-06-14T13:49:52+00:00

We have a set of domain classes which are serialized to json via jackson

  • 0

We have a set of domain classes which are serialized to json via jackson using jersey services. We are currently annotating the classes with JAXB (although we’re not tied to that). This works fine. But we want to offer different serializations of the classes for different use cases.

  • Web site
  • Mobile apps
  • Admin tool
  • Public API

In each of these cases there are different fields which we may or may not want included in the json view. For example, the admin tool might need some parameters for setting permissions on data. The mobile client needs a different URL to a media stream than the website. The website has particular naming conventions it needs for fields.

What is the best practice for managing different mappings of json for different service endpoints in Jersey?

Thanks!

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

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    MOXy offers JSON-binding based on JAXB annotations as well as an external binding document that allows you to apply alternate mappings to a domain model. I will demonstrate below with an example.

    Metadata as JAXB Annotations

    Below is a simple Java model mapping with the standard JAXB annotations.

    package forum10761762;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer {
    
        int id;
    
        @XmlElement(name="first-name")
        String firstName;
    
        @XmlElement(name="last-name")
        String lastName;
    
    }
    

    Alternate Metadata #1 (alternate1.xml)

    Here we will use the XML mapping document to unmap a couple of fields by making them @XmlTransient.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum10761762">
        <java-types>
            <java-type name="Customer">
                <java-attributes>
                    <xml-transient java-attribute="id"/>
                    <xml-transient java-attribute="firstName"/>
                 </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Alternate Metadata #2 (alternate2.xml)

    Here we will map the Java model to a different JSON structure using MOXy’s path based mapping extension.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum10761762">
        <java-types>
            <java-type name="Customer">
                <java-attributes>
                    <xml-element java-attribute="firstName" xml-path="personalInfo/firstName/text()"/>
                    <xml-element java-attribute="lastName" xml-path="personalInfo/lastName/text()"/>
                 </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo Code

    package forum10761762;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Customer customer = new Customer();
            customer.id = 123;
            customer.firstName = "Jane";
            customer.lastName = "Doe";
    
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    
            // Output #1
            JAXBContext jc1 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
            marshal(jc1, customer);
    
            // Output #2
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum10761762/alternate1.xml");
            JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
            marshal (jc2, customer);
    
            // Output #2
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum10761762/alternate2.xml");
            JAXBContext jc3 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
            marshal(jc3, customer);
        }
    
        private static void marshal(JAXBContext jc, Object object) throws Exception {
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(object, System.out);
            System.out.println();
        }
    
    }
    

    Output

    Below is the output from running the demo code. Note from the same object model 3 different JSON documents were produced.

    {
       "id" : 123,
       "first-name" : "Jane",
       "last-name" : "Doe"
    }
    {
       "last-name" : "Doe"
    }
    {
       "id" : 123,
       "personalInfo" : {
          "firstName" : "Jane",
          "lastName" : "Doe"
       }
    }
    

    For More Information (from my blog)

    • JSON Binding with EclipseLink MOXy – Twitter Example
    • MOXy as Your JAX-RS JSON Provider – MOXyJsonProvider
    • MOXy’s XML Metadata in a JAX-RS Service
    • Specifying EclipseLink MOXy as Your JAXB Provider
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some of my PHP domain objects have properties which should be set from outside
We're using interfaces to represent entity classes in our domain model. We have concrete
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
If I have domain name example.com , and if I set an A record
Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int
I have set up upload_max_filesize and post_max_size to 32Mb in php.ini. I am using
I am trying to set up proper domain architecture using Fluent NHibernate and Linq
I have two domain classes with a many-2-many relationship between them, e.g. User and
I have an existing database, which I have been happily accessing using LINQtoSQL. Armed
I have some domain classes that look something like this, that I want 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.