I'm trying to test JAXB unmarshaller/marshaller. Here is my code
JAXBContext context = JAXBContext.newInstance(ClientUser.class.getPackage().getName());
And a code of my entity
@XmlRootElement(name = "user")
public class ClientUser {
private String name;
public ClientUser() {}
public ClientUser(String name) {
this.name = name;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Even if I add to the entity class a factory class
@XmlRegistry
class ObjectFactory {
ClientUser createPerson() {
return new ClientUser();
}
}
I'm still keep getting this exception
Exception in thread "main" javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "com.example.ws.poc.entity" doesnt contain ObjectFactory.class or jaxb.index
- with linked exception:
[javax.xml.bind.JAXBException: "com.example.ws.poc.entity" doesnt contain ObjectFactory.class or jaxb.index]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:335)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:298)
How can I fix this error?
A JAXB implementation doesn't do package scanning. If you boot strap from a package name, JAXB is going to look for an ObjectFactory
(annotated with @XmlRegistry
) or a jaxb.index
file that contains short class names each on a new line.
If you don't have these two items you can create the JAXBContext
on the domain classes themselves.
JAXBContext jc = JAXBContext.newInstance(Foo.class, Bar.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With