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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:40:31+00:00 2026-05-29T05:40:31+00:00

Edit: I originally left out what may be an important detail from my question

  • 0

Edit: I originally left out what may be an important detail from my question — My service methods that return Java objects to be marshaled return an interface type (Foo) rather than the class implementation type (FooImpl).

I have a simple Java class with JAX-B annotations for several elements and attributes:

@XmlRootElement(name = "foo")
public class FooImpl {
    private String id;
    private String name;

    @XmlElement
    public String getName() {
            return name;
    }

    public void setName(final String name) {
            this.name = name;
    }

    @XmlAttribute
    public String getId() {
            return Id;
    }

    public void setId(final String id) {
            this.id = id;
    }
}

Edit: The FooImpl class has an interface named Foo:

public interface Foo {
    public String getName();
    public void setName(final String name);

    public String getId();
    public void setId(final String id);
}

When I have a service method that returns a Foo, I get what I expect:

<foo id="abc123">
  <name>bar</name>
</foo>

But I also have another class that contains a List<Foo> and when it is marshalled, the XML elements for foo do not contain their id attribute!!

<foos>
    <foo>
      <name>bar1</name>
    </foo>
    <foo>
      <name>bar2</name>
    </foo>
</foos>

The class that holds the list looks like this:

@XmlRootElement(name = "foos")
public class Foos {

    private List<Foo> foos;

    @XmlElement(name = "foo")
    public List<Foo> getFoos() {
        return foos;
    }

    public void setFoos(List<Foo> foos) {
        this.foos = foos;
    }

}

I happen to be using MOXy as my JAX-B implementation, but I do not think that matters.

  • 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-05-29T05:40:32+00:00Added an answer on May 29, 2026 at 5:40 am

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

    Since you have a property whose type is an interface you will need to specify the implementing type on the @XmlElement annotation:

    @XmlElement(name = "foo", type=FooImpl.class)
    public List<Foo> getFoos() {
       return foos;
    }
    

    Below is a complete example:

    Foos

    package forum9137171;
    
    import java.util.List;  
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name = "foos")
    public class Foos {
    
        private List<Foo> foos;
    
        @XmlElement(name = "foo", type=FooImpl.class)
        public List<Foo> getFoos() {
            return foos;
        }
    
        public void setFoos(List<Foo> foos) {
            this.foos = foos;
        }
    
    }
    

    Foo

    package forum9137171;
    
    public interface Foo {
        public String getName();
        public void setName(final String name);
    
        public String getId();
        public void setId(final String id);
    }
    

    FoomImpl

    package forum9137171;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name = "foo")
    public class FooImpl implements Foo {
        private String id;
        private String name;
    
        @XmlElement
        public String getName() {
                return name;
        }
    
        public void setName(final String name) {
                this.name = name;
        }
    
        @XmlAttribute
        public String getId() {
                return id;
        }
    
        public void setId(final String id) {
                this.id = id;
        }
    }
    

    Demo

    package forum9137171;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Foos.class);
            System.out.println(jc);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum9137171/input.xml");
            Foos foos = (Foos) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foos, System.out);
        }
    
    }
    

    Input/Output

    org.eclipse.persistence.jaxb.JAXBContext@16a786
    <?xml version="1.0" encoding="UTF-8"?>
    <foos>
       <foo id="abc123">
          <name>bar1</name>
       </foo>
       <foo id="def456">
          <name>bar2</name>
       </foo>
    </foos>
    

    For More Information

    • http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: From another question I provided an answer that has links to a lot
Edit: original question below, but I revise it now that I have some code
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
this is an edit of the original post now that I better understand the
Edit: This question was written in 2008, which was like 3 internet ages ago.
I am building an iPhone app that plays videos on demand from a web
Edit: Worked out the problem - see accepted answer for a good explanation! My
[EDITED: I left the original question below, with some more context and code to
Edit: original title convert numpy array to cvmat was a mistake - OpenCV's less
[community edit: original title was python conditionals, OP is asking what is wrong with

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.