First: Your XmlAdapter incorrect. Common types are the other way around.
Then you seem to need to put @XmlJavaTypeAdapter on CustomRootElement .
In addition, JAXBContext needs to be told about all the classes involved. Create jaxb.index or ObjectFactory and create a context by specifying the package name in the newInstance method or listing all the classes.
Full code (slightly modified since I use main() , not the JUnit test method):
public static class CustomType { } public static class CustomTypeAdapter extends XmlAdapter<String, CustomType> { @Override public String marshal(CustomType v) throws Exception { return "CustomType"; } @Override public CustomType unmarshal(String v) throws Exception { return new CustomType(); } } public static class RootElement<V> { public V value; } @XmlJavaTypeAdapter(CustomTypeAdapter.class) @XmlRootElement(name = "root") public static class CustomRootElement extends RootElement<CustomType> { public CustomRootElement() { value = new CustomType(); } } public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(CustomRootElement.class, CustomType.class, RootElement.class); StringWriter w = new StringWriter(); CustomRootElement cre = new CustomRootElement(); cre.value = new CustomType(); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); marshaller.marshal(cre, w); System.err.println(w.toString());
Now the result of sorting a CustomRootElement is not what you expect in your test (and this is not what I expected), but you can untie it and get what you previously planned. So, (un) sorting both works, but XML doesn't look so good:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <value xsi:type="customType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> </root>
I also put the field in CustomType and it worked too. Therefore, if you do not need good XML, this solution should be sufficient. Hope I have not forgotten any changes I made. If I did, just leave a comment and I will edit accordingly.
source share