I found another difference between Jaxb processing in Java 7 and Java 8. I reduced the problem to a simplified example, and the code should work as a single class. (changed categories so that they are not related to work, etc.). When the installer for the list is called by Unmarshaller: My question is really a variation
- Recommended practice for coding List accessers to omit Setter at all when coding JaxB? (since it seems to be handling the List via Getter)
- Are alternative approaches recommended?
When launched on Java 7, the installer will be called with the data in the List. When launched on Java 8, the installer will only be called with an empty List object, which is likely to be populated later in the unmarshalling process. The difference I'm experiencing is that I should not have a customization tool for any processing on the List, but rather, it has a process that is called only after the shared object is unreinforced. Or to summarize: "do not do the processing in the setter." An example is given below: (Three classes). In Java 7, the return result is the first name "album" in the list found in the installer. In Java 8, null is returned. The code should work as one class without dependencies. If "First Album" is displayed in Java 7, "Abbey Road" is displayed. If in Java 8 the header is "First album is null
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
public class MainJaxbCase {
public static void main( String[] args ) {
new MainJaxbCase().testIt();
}
private void testIt() {
try {
AlbumLib myLib = (AlbumLib) loadJaxbDocFromString( inXmlStr, AlbumLib.class );
System.out.println("category:"+ myLib.getCateg());
List<AlbumItm> albumList = myLib.getAlbumList();
System.out.println("AlbumList size is " + albumList.size());
System.out.println("The first album is titled:"
+ myLib.getFirstAlbumTitle()
+ "- shows \"null\" if using Java 8, \"Abbey Road\" if using Java 7"
);
} catch ( Exception e ) {
System.out.println( e.getClass().getSimpleName() + ", msg:" + e.getMessage() );
e.printStackTrace();
}
}
private final String inXmlStr =
"<my_lib categ='albums'>"
+ " <album title='Abbey Road'/> "
+ " <album title='Revolver'/>"
+ " <album title='Sgt.Pepper'/>"
+ "</my_lib>";
private Object loadJaxbDocFromString ( String inStr, Class<?> clazz ) throws Exception {
Object result = null;
try {
InputStream is = new ByteArrayInputStream( inStr.getBytes() );
result = unmarshal( is, clazz );
} catch ( Exception e ) {
String msg = this.getClass().getSimpleName() + ".loadJaxbDocFromResource() caught " + e.getClass().getSimpleName() + " msg:" + e.getMessage();
throw new Exception(msg);
}
return result;
}
private Object unmarshal( InputStream prmIs, Class<?> clazz ) throws Exception{
Object obj = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance( clazz );
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
obj = jaxbUnmarshaller.unmarshal( prmIs );
} catch ( Exception e ) {
String msg = this.getClass().getSimpleName() + " caught " +
e.getClass().getSimpleName() + ", msg:" + e.getMessage();
msg += " Trying to Unmarshall class " + clazz.getName();
System.err.println(msg);
e.printStackTrace();
throw new Exception(msg);
}
return obj;
}
}
@XmlRootElement ( name= "my_lib")
class AlbumLib {
private String categ;
private List<AlbumItm> albumList;
private String firstAlbumTitle;
@XmlAttribute ( name="categ")
public String getCateg() {
return this.categ;
}
public void setCateg( String val ) {
this.categ=val;
}
@XmlElement ( name="album")
public List<AlbumItm> getAlbumList() {
return this.albumList;
}
public void setAlbumList( List<AlbumItm> newList ) {
if ( newList != null && newList.size() > 0 ) {
firstAlbumTitle = newList.get(0).getTitle();
}
this.albumList = newList;
}
public String getFirstAlbumTitle() {
return this.firstAlbumTitle;
}
}
@XmlType(name = "album")
class AlbumItm {
private String title;
@XmlAttribute ( name="title" )
public String getTitle() {
return this.title;
}
public void setTitle(String val ) {
this.title = val;
}
}
, , ( ) , Java 8, - .