I try to parse a XML(String) to a Object using JAXB, but I can't find the way to call the method I had created because I don't know how to specify the class BasicRequest<G>
with the SomeRequest
as G
public class XMLParser {
public <G extends BaseRequest> BasicRequest<G> xmlToRequest(String xml,
Class<BasicRequest<G>> t) {
BasicRequest<G> request = null;
try {
JAXBContext context = JAXBContext.instance(t);
Unmarshaller m = context.createUnmarshaller();
StringReader reader = new StringReader(xml);
JAXBElement<BasicRequest<G>> unmarshal = m.unmarshal(new
StreamSource(reader), t);
request = unmarshal.getValue();
} catch (JAXBException ex) {
//write log
}
return request;
}
}
This are my classes:
public abstract class BaseRequest {
protected String version;
//getter & setter
}
,
public class SomeRequest extends BaseRequest {
protected Integer id;
//getter & setter
}
and
public class BasicRequest<G extends BaseRequest> {
protected String user;
protected G data;
//getter & setter
}
All classes actually have the JAXB annotation correctly.
If someone could please give me a hand on how should I call the method or a better way to define it I would appreciate.