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

The Archive Base Latest Questions

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

Hopefully an easy one for JAXB experts: I am trying to marshal an immutable

  • 0

Hopefully an easy one for JAXB experts:

I am trying to marshal an immutable class that does not define a default no-arg constructor. I have defined an XmlAdapter implementation but it doesn’t seem to be picked up. I have put together a simple self-contained example, which is still failing to work. Can anyone advise what I’m doing wrong?

Immutable Class

@XmlJavaTypeAdapter(FooAdapter.class)
@XmlRootElement
public class Foo {
  private final String name;
  private final int age;

  public Foo(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() { return name; }
  public int getAge() { return age; }
}

Adapter and Value Type

public class FooAdapter extends XmlAdapter<AdaptedFoo, Foo> {
  public Foo unmarshal(AdaptedFoo af) throws Exception {
    return new Foo(af.getName(), af.getAge());
  }

  public AdaptedFoo marshal(Foo foo) throws Exception {
    return new AdaptedFoo(foo);
  }
}

class AdaptedFoo {
  private String name;
  private int age;

  public AdaptedFoo() {}

  public AdaptedFoo(Foo foo) {
    this.name = foo.getName();
    this.age = foo.getAge();
  }

  @XmlAttribute
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  @XmlAttribute
  public int getAge() { return age; }
  public void setAge(int age) { this.age = age; }
}

Marshaller

public class Marshal {
  public static void main(String[] args) {
    Foo foo = new Foo("Adam", 34);

    try {
      JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

      // output pretty printed
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      jaxbMarshaller.marshal(foo, System.out);              
    } catch (JAXBException e) {
      e.printStackTrace();
    }   
  }
}

Stack Trace

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Foo does not have a no-arg default constructor.
        this problem is related to the following location:
                at Foo

        at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:283)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
        at Marshal2.main(Marshal2.java:11)

Note that I am using JDK 1.7.0_05.

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

    The following should help:

    FOO AS ROOT OBJECT

    When @XmlJavaTypeAdapter is specified at the type level it only applies to fields/properties referencing that class, and not when an instance of that class is a root object in your XML tree. This means that you will have to convert Foo to AdaptedFoo yourself, and create the JAXBContext on AdaptedFoo and not Foo.

    Marshal

    package forum11966714;
    
    import javax.xml.bind.*;
    
    public class Marshal {
        public static void main(String[] args) {
          Foo foo = new Foo("Adam", 34);
    
          try {
            JAXBContext jaxbContext = JAXBContext.newInstance(AdaptedFoo.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            jaxbMarshaller.marshal(new AdaptedFoo(foo), System.out);              
          } catch (JAXBException e) {
            e.printStackTrace();
          }   
        }
      }
    

    AdaptedFoo

    You will need to add an @XmlRootElement annotation to the AdaptedFoo class. You can remove the same annotation from the Foo class.

    package forum11966714;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    class AdaptedFoo {
        private String name;
        private int age;
    
        public AdaptedFoo() {
        }
    
        public AdaptedFoo(Foo foo) {
            this.name = foo.getName();
            this.age = foo.getAge();
        }
    
        @XmlAttribute
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @XmlAttribute
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    FOO AS NESTED OBJECT

    When Foo isn’t the root object everything works the way you have it mapped. I have extended your model to demonstrate how this would work.

    Bar

    package forum11966714;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Bar {
    
        private Foo foo;
    
        public Foo getFoo() {
            return foo;
        }
    
        public void setFoo(Foo foo) {
            this.foo = foo;
        }
    
    }
    

    Demo

    Note that the JAXB reference implementation will not let you specify the Foo class when bootstrapping the JAXBContext.

    package forum11966714;
    
    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
        public static void main(String[] args) {
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance(Bar.class);
    
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                File xml = new File("src/forum11966714/input.xml");
                Bar bar = (Bar) jaxbUnmarshaller.unmarshal(xml);
    
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                jaxbMarshaller.marshal(bar, System.out);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
        }
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <bar>
        <foo name="Jane Doe" age="35"/>
    </bar>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this will be an easy one to answer. I created a class in
Hopefully, this is an easy one. I have an array with lines that contain
hopefully this is an easy one. I have a query that I want to
Hopefully an easy one, I have created a Custom Repeater control that extends System.Web.UI.WebControls.Repeater.
Hopefully an easy one for you... I've been trying to set the background color
Hopefully this is an easy one... I have an NSObject with methods that is
Nice and (hopefully) easy. I am trying to work out how to grab the
Hopefully this is an easy one. Is there a way to test for an
hopefully there's an easy solution to this one. I have my MVC2 project which
Hopefully this is a quick one, and Easy if you know how... I'm writing

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.