Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Collections (List<T>) Use Pascal Case instead of Camel Case for Element Names

Tags:

java

jaxb

jersey

I have a number of JAXB beans that are directly marshalled and unmarshalled using Jersey.

E.g.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Artifact", propOrder = {
    "artifactId",
    "artifactType",
    "contentHash"
})
@XmlSeeAlso({
    EmailArtifact.class,
    ManagedDocArtifact.class
})
@XmlRootElement(name ="Artifact")
public class Artifact {

    protected String artifactId;
    protected String artifactType;
    protected String contentHash;

...
...
...

}

If I create a GET method that returns a single artifact object. It correctly produces the XML:

<Artifact>
 <artifactId>293289392839283</artifactId>
 <artifactType>EMAIL</artifactType>
 <contentHash>2837873827322</contentHash>
</Artifact>

Here I have been able to successfully control the name of the Artifact element to have a capital "A" at the beginning.

However, I create a GET method that returns collection of artifact objects, I end up with:

<artifacts>
<Artifact>
 <artifactId>293289392839283</artifactId>
 <artifactType>EMAIL</artifactType>
 <contentHash>2837873827322</contentHash>
</Artifact>
<Artifact>
 <artifactId>293289392839283</artifactId>
 <artifactType>EMAIL</artifactType>
 <contentHash>2837873827322</contentHash>
</Artifact>
</artifacts>

As you can see the outer element for the collection has a lower case "A". In order to conform to our own internal standard I would like this to be a capital "A" - Artifacts.

I can not see where this is possible to define within JAXB, is it actually the Jersey framework that is controlling this?

Can we control of the element name generated for collections?

Many thanks,

James

like image 679
jallch Avatar asked Oct 09 '09 17:10

jallch


People also ask

What is the difference between camel case and Pascal case?

Camel case and Pascal case are similar. Both demand variables made from compound words and have the first letter of each appended word written with an uppercase letter. The difference is that Pascal case requires the first letter to be uppercase as well, while camel case does not.

What is camelCase and Pascal case in C#?

Camel Case (camelCase): In this standard, the first letter of the word always in small letter and after that each word starts with a capital letter. Pascal Case (PascalCase): In this the first letter of every word is in capital letter.

What is Pascal case naming convention?

PascalCase is a naming convention in which the first letter of each word in a compound word is capitalized. Software developers often use PascalCase when writing source code to name functions, classes, and other objects. PascalCase is similar to camelCase, except the first letter in PascalCase is always capitalized.

What is camelCase example?

Meaning of camel case in English. the use of a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space: Examples of camel case include "iPod" and "GaGa".


2 Answers

Try using @XmlElementWrapper which supports the name attribute:

@XmlElementWrapper(name="ELEMENTS")
@XmlElement(name="ELEMENT")
protected final List<String> elements = new LinkedList<String>();

Will produce

<ELEMENTS>
  <ELEMENT>one</ELEMENT>
  <ELEMENT>two</ELEMENT>
  ...
</ELEMENTS>

See the JAXB Tutorial for this feature.

like image 152
java.is.for.desktop Avatar answered Nov 15 '22 13:11

java.is.for.desktop


OK - I have had an update from the Java Jersey community leader - Paul Sandoz. This is currently an open issue with the Jersey framework and just something that we will have to workaround or accept until it is fixed in a future update.

The workaround as explained in one of the comments above is to introduce a new class for each web service method that returns a collection (e.g. List) of JAXB annotated beans. This class is effectively a class that contains just one member - a List of the JAXB beans. Instead of returning the List in the web service method we return the special class instead. Because we have control over the name of the new class in the XML, e.g. @XmlRootElement(name="Artifacts") we can workaround the problem in the short term.

Hope this helps.

Regards,

James

like image 32
jallch Avatar answered Nov 15 '22 13:11

jallch