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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:21:37+00:00 2026-06-06T14:21:37+00:00

I’m struggling with the few problems marshal/unmarshall JSon and it looks i will never

  • 0

I’m struggling with the few problems marshal/unmarshall JSon and it looks i will never get it work 🙁

Look the first issue: I have a class Response that contains few fields: String error, String error_code and Object response. Class responsible to fill all the data to response, fill the error fields if needed and send it back to the client for further analisys.

“Object response” can be any of type (why Object? because it didn’t worked with interfaces and that would be the second part of the question)

code snippet:

   @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Response
    {
        public static final String ERROR_FIELD_NAME = "error";
        public static final String ERROR_CODE_FIELD_NAME = "error_code";
        public static final String RESPONSE_FIELD_NAME = "response";

        @XmlElement(name = Response.ERROR_CODE_FIELD_NAME)
        private String errorCode;

        @XmlElement(name = Response.ERROR_FIELD_NAME)
        private String errorMessage;

        @XmlElementRefs(
            {@XmlElementRef(type = AClassResponse.class),
        @XmlElementRef(type = BClassResponse.class)})
        private Object entity;

and etc…

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;


//@XmlRootElement(name = Response.RESPONSE_FIELD_NAME)
//@XmlJavaTypeAdapter(AClassResponse.Adapter.class)
@XmlRootElement
public class AClassResponse
{
    private TargetObject target;


    public TargetObject getTarget()
    {
        return target;
    }


    public void setTarget(TargetObject target)
    {
        this.target = target;
    }
}

JSON 1 (for XmlElementRef AClassResponse.class ):
    { 
      "error" : "",
      "error_code" : "",
      **"aClassResponse" :** 
         { "target" : 
            { "accountId" : "5bd0812b",
              "id" : "6ccb6ae1-30c3-4aa0-8d1e-6ed61b6bb217",
              "isCaseSensitive" : "false",
              "maxNumberOfConnections" : "0",
              "name" : "targetName",
              "pathDelimiter" : "/",
              "type" : "SMB"
            } 
         }
    }

JSON 1 (for XmlElementRef BClassResponse.class):
   { 
      "error" : "",
      "error_code" : "",
      **"bClassResponse" :** 
         { "target" : 
            { "accountId" : "5bd0812b",
              "id" : "6ccb6ae1-30c3-4aa0-8d1e-6ed61b6bb217",
              "isCaseSensitive" : "false",
              "maxNumberOfConnections" : "0",
              "name" : "targetName",
              "pathDelimiter" : "/",
              "type" : "SMB"
            } 
         }
    }

Now the problem:

Look on JSon above, “aClassResponse” : and “bClassResponse” : are always dynamic (means the name is derived from the class name) – an this is not what I need.

My goal is to give this name a persistent name “response”, so any remote clients will aware about this and rely on this name.

I know, to fix that I can annotate AClassResponse.class with @XmlRootElement(name = “response”), and it does work untill you have more than one @XmlElementRef defined.

If two or more, this cause an issue with marshaling/unnmarshaling, and JAXB doesn’t know what class to bind it should bind to the entity field, since it rely on name as well (name = “response”)

@XmlElementRefs(
        {@XmlElementRef(type = AClassResponse.class),
        @XmlElementRef(type = BClassResponse.class)})
        private Object entity;

it treat it like this:

@XmlElementRefs(
            {@XmlElementRef(name = "response", type = AClassResponse.class),
            @XmlElementRef(name = "response", type = BClassResponse.class)})
            private Object entity;

Assume this a correct behaviour and there is nothing i can do to keep the the same (name = “response”) for any of object types.

OK, i tried to workaround it by introducing interface instead of Object type + @XmlJavaTypeAdapter(AClassResponse.Adapter.class) for each of implementation – however it didn’t worked either, throwing exception that interfaces are not supported and etc…

This article seems can shed some light on this, http://jaxb.java.net/guide/Mapping_interfaces.html

but it fails too, saying that it can’t instantiate abstract class abstract class AbstractFooImpl

@XmlJavaTypeAdapter(AbstractFooImpl.Adapter.class)
interface IFoo {
  ...
}
**abstract class AbstractFooImpl implements IFoo {**
  ...

  static class Adapter extends XmlAdapter<AbstractFooImpl,IFoo> {
    IFoo unmarshal(AbstractFooImpl v) { return v; }
    AbstractFooImpl marshal(IFoo v) { return (AbstractFooImpl)v; }
  }
}

class SomeFooImpl extends AbstractFooImpl {
  @XmlAttribute String name;
  ...
}

class AnotherFooImpl extends AbstractFooImpl {
  @XmlAttribute int id;
  ...
}

class Somewhere {
  public IFoo lhs;
  public IFoo rhs;
}

Any advices on how i can workaround this? Please help..

thank you.

  • 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-06T14:21:39+00:00Added an answer on June 6, 2026 at 2:21 pm

    JAXB doesn’t do interfaces. The solution is either generics or abstract classes (annotated with @XmlTransient to make JAXB ignore the base class and @XmlSeeAlso to make it use specific implementations instead). That solves the Java part, but you still need to fix the XSD part of the equation (remember that JAXB only sees an XML version of your JSON through Jersey).

    For that purpose you can use the @XmlElementRef and @XmlElementRefs annotations on your field so that the XSD will map to a choice of the specific implementations.

    @XmlTransient // JAXB doesn't like abstract types, so do not map this type name
    // XmlSeeAlso only required if BaseImpl/BaseImpl2 are not otherwise auto-discovered by JAXB
    @XmlSeeAlso({BaseImpl.class, BaseImpl2.class })
    public abstract class Base {
      @XmlElement(name='still-works')
      public String thisFieldWillStillBeMappedByJAXB;
    }
    
    @XmlRootElement("base-impl")
    public class BaseImpl {}
    
    @XmlRootElement("alternative")
    public class BaseImpl2 {}
    
    public class Complex<E extends Base> {
       @XmlElementRefs({
         @XmlElementRef(type=BaseImpl.class), 
         @XmlElementRef(type=BaseImpl2.class)
       })
       public E impl; // will be an instance of either "base-impl" or "alternative"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.