1

I am learning JAX-WS and i also was introduced with JAXB. I can use it to (un)marshal java(xml) to xml(java). But what is the purpose of this line then i generated my WS data with Apache CXF:

@XmlElementRef(name = "Id", namespace = "External.Test", type = JAXBElement.class, required = false)
    protected JAXBElement<String> id;
  1. Why it can't be just String id instead of JAXBElement<String> id ?
  2. How can I convert request data (let's say java POJO) into structure like this above? (I am talking about mapping from POJO to this exposed WS structure with fields wrapped into JAXBElements)
energizer
  • 91
  • 6
  • It can't just be a string because you are dealing with a web service. Web services are used via different languages, and some languages handle strings differently. Take a look at this http://stackoverflow.com/questions/14489306/parameter-jaxbelement-string – j.con Sep 30 '14 at 19:41
  • @j.con This has nothing to do with string handling in different languages. – lexicore Sep 30 '14 at 21:34
  • @lexicore oh yea? Try to consume an `int` from a java web service in C# when the `int` is null. C# does not allow `int` to be null, only a `int?` that is why a standard wrapper is used... – j.con Oct 01 '14 at 12:40
  • @j.con I repeat, `@XmlElementRef` or `JAXBElement` vs. `String` has nothing to do with string handling in different languages. This depends on how the originating schema is defined. The problem you're addressing is the nillability. Well, yes, `nillable="true"` is ONE of the reasons for `@XmlElementRef`, but it is not the only one. – lexicore Oct 01 '14 at 12:54

1 Answers1

1

1.- This structure is used depending on how did you define your schema. For instance, if you use a sustitution group. See this answer for more info.

2.- Here some code that might help you: Let´s say that you POJO defines a structure like this:

public JAXBElement<?> getObjectData() {
    return objectData;
}

Then, when you unmarshalled the request that you have received, is filled with a String that you want to recover:

JAXBElement<String> rncElem = (JAXBElement<String>) dataContainer.getObjectData();
return rncElem.getValue();
Community
  • 1
  • 1
Victor
  • 2,450
  • 2
  • 23
  • 54