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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:03:25+00:00 2026-06-01T23:03:25+00:00

I have a complex hierarchy of Java interfaces that I’d like to marshal (and

  • 0

I have a complex hierarchy of Java interfaces that I’d like to marshal (and not necessarily unmarshal) with JAXB. These interfaces represent objects that will be returned from a JAX-RS REST API as XML, JSON, YAML, etc. (I’m using RestEasy, which can marshal JAXB-annotated types in formats other than XML.)

The problem seems to be that JAXB is fundamentally class-oriented. I’ve done lots of web research on the difficulties with JAXB and interfaces, the closest solutions being MOXy JAXB – Map Interfaces to XML and JAXB and Interface Fronted Models. However, I have two main issues: a) I want to annotate/work in terms of interfaces, not the concrete classes (of which there will be multiple implementations and containing significant other state that should not be marshaled), and b) I have multiple levels of interface inheritance. Here’s a sample of the interfaces, minus any JAXB annotations so far:

interface Uuided {
  UUID getId();
}
interface Named {
  String getName();
}
interface Component extends Uuided, Named {
  Map<String, ComponentAttribute> getAttributes();
}
interface Attribute extends Named {
  Type getType();
  Object getValue();
}
interface ComponentAttribute extends Attribute {
  Component getDeclaringComponent();
}

Ideally, this would generate something like:

<component id="xxx" name="thing">
  <attributes>
    <componentAttribute name="color">
      <type><stringType/></type>
      <value>green</value>
      <declaringComponent idref="xxx"/>
    </componentAttribute>
  </attributes>
</component>

Clearly, in the abstract, this leads to issues like determining the most derived annotated interface, of which there could theoretically be more than one. In my case, however, I believe the concrete classes only implement a single interface that should be marshaled. Unmarshaling is not necessary, as I have separate classes defining upsert properties.

So my question is, is this even possible to handle with JAXB, and if so, how? Even if I have to be very explicit in defining bindings, adapters, etc., I’d like to work within the JAXB framework to get the benefit of all the non-XML providers in RestEasy.

  • 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-01T23:03:26+00:00Added an answer on June 1, 2026 at 11:03 pm

    I think the answer is that JAXB isn’t at all intended to support this and that it’s foolish to try to force it. Also, JAXB-driven JSON marshaling turns out not to be ideal either.

    I ended up writing my own marshaling framework, with its own set of annotations:

    @MarshalMixin // marshal fields but not a top-level object
    interface Uuided {
      @MarshalAsString // ignore properties; just call toString()
      @MarshalId // treat as identifier for @MarshalUsingIds or cyclic ref
      UUID getId();
    }
    @MarshalMixin
    interface Named {
      @MarshalId
      String getName();
    }
    @MarshalObject // top-level marshaled object providing class name
    interface Component extends Uuided, Named {
      @MarshalAsKeyedObjectMap(key = "name") // see description below
      Map<String, ComponentAttribute> getAttributes();
    }
    @MarshalObject
    interface Attribute extends Named {
      Type getType();
      @MarshalDynamic // use run-time (not declared) type
      Object getValue();
    }
    interface ComponentAttribute extends Attribute {
      @MarshalUsingIds
      Component getDeclaringComponent();
    }
    

    The generated marshalers write to an abstraction layer (currently implemented for JSON and XML). This gives a lot more flexibility to make the output natural for different representations without a ton of annotations and adapters. E.g., what I’m calling keyed-object maps, where each object contains its map key. In JSON, you want a map, but in XML, you want a sequence:

    {..., map: {'a': {'name': 'a', ...}, 'b': {'name: 'b', ...}, ...}, ...}
    ...<map><thing name='a'>...</thing><thing name='b'>...</thing></map>...
    

    Seems like as many as 4 other people care about this too, so hopefully I can open-source it eventually. 🙂

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

Sidebar

Related Questions

I have a complex hierarchy of objects that we can represent, for illustration purpose,
I have a quite complex class hierarchy in which the classes are cross-like depending
I have a complex object hierarchy that I've been wrestling with to get passed
I have several complex data structures like Map< A, Set< B > > Set<
I have a complex RIA client that communicates with a WCF SOAP web service,
I have a fairly complex WordPress + bbPress installation and I would like to
I have a hierarchy of directories containing many text files. I would like to
I have a widget that displays a filesystem hierarchy for convenient browsing (basically a
I have a complex data model (for EF 4.1 code first) that uses composition
I have a hierarchy of processes called monitor_node. Each of these monitor_nodes is supervised

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.