JAXB ValidationEventHandler's handleEvent method not being called
Initially suggested to replace
JAXBContext context = JAXBContextFactory.createContext(new Class[]{Article.class}, null);
to
JAXBContext context = JAXBContext.newInstance(Article.class);
In the implementation of JAXBContextFactory.createContext you can see that the classesToBeBound, which in your case you pass Article, perform some checks which result in exception for being "Unable to find a JAXB implementation to delegate". This is the problem I had when running your code and I assumed you had as well. Then your event handler was called (at least in my setup).
Later you had issues with "... Class has two properties of the same name "value" ..." and I proposed to check this link which explains the reason it happens.
the link is: What is the difference between using @XmlElement before field and before getter declaration?
Edit to reply to new question:
If you don't mind me asking what are you trying to do with @XmlPath("image")
?
Your POJO structure does not match the xml. Remove the annotation and change the setter method for image, as below:
@XmlRootElement
class Article {
private String title;
private String category;
private List<ArticleImage> imageList;
public String getTitle() {
return title;
}
@XmlElement
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
@XmlElement
public void setCategory(String category) {
this.category = category;
}
public List<ArticleImage> getImage() {
return imageList;
}
// for Extra Question... :D
// method name changed!
public void setImage(List<ArticleImage> imageList) {
this.imageList = imageList;
}
}
class ArticleImage {
private String url;
private String ext;
public String getUrl() {
return url;
}
@XmlAttribute
public void setUrl(String url) {
this.url = url;
}
public String getExt() {
return ext;
}
@XmlAttribute
public void setExt(String ext) {
this.ext = ext;
}
}
As told by @mart and @Sergey
Update JAXBContext context = JAXBContextFactory.createContext(new Class[]{Article.class}, null);
JAXBContext context = JAXBContext.newInstance(Article.class);
And Also Add @XmlAccessorType(XmlAccessType.FIELD) in your Article pojo class.
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Article {
@XmlElement
private String title;
@XmlElement
private String category;
//setter getters
}
I have checked it,In case of exception, CustomValidationHandler is being called.
it works for both cases: InputStream and StringReader
just changed that:
JAXBContext context = JAXBContext.newInstance(Article.class);