Estou estudando Java, e na apostila que leio, é sugerida a utilização da biblioteca XStream para leitura de XML. Estou tentando fazer a leitura de uma string xml para um bean que possui um atributo data, do tipo LocalDate (do Java 8). Como posso formatar essa minha String para que essa conversão possa ser realizada?
Estou tentando desta forma:
String xml = "<list>" +
"<negociacao>" +
"<valor>43.5</valor>" +
"<quantidade>1000</quantidade>" +
"<data>18/04/2016</data>" +
"</negociacao>" +
"</list>";
XStream xstream = new XStream(new DomDriver());
xstream.alias("negociacao", Negociacao.class);
List<Negociacao> lista = (List<Negociacao>) xstream.fromXML(xml);
Mas recebo o seguinte erro:
com.thoughtworks.xstream.converters.ConversionException: Cannot deserialize object with new readObject()/writeObject() methods
---- Debugging information ----
message : Cannot deserialize object with new readObject()/writeObject() methods
class : java.time.LocalDate
required-type : java.time.LocalDate
converter-type : com.thoughtworks.xstream.converters.reflection.SerializableConverter
path : /list/negociacao/data
class[1] : argentum.model.Negociacao
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2] : java.util.ArrayList
converter-type[2] : com.thoughtworks.xstream.converters.collections.CollectionConverter
version : 1.4.9
-------------------------------
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.doUnmarshal(SerializableConverter.java:318)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:73)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:98)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:91)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:85)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:80)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
at argentum.model.LeitorXml.carrega(LeitorXml.java:15)
at argentum.model.LeitorXmlTest.carregaUmaNegociacao(LeitorXmlTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Meu bean Negociacao:
package argentum.model;
import java.time.LocalDate;
/**
* @author Vinicius Dias
*/
public final class Negociacao {
private final double valor;
private final double quantidade;
private final LocalDate data;
public Negociacao(double valor, double quantidade, String data) throws IllegalArgumentException {
LocalDate localDate;
if (data.indexOf("/") > -1) {
String[] date = data.split("/");
if (date.length != 3) {
throw new IllegalArgumentException("Data inválida");
}
localDate = LocalDate.of(
Integer.parseInt(date[2]),
Integer.parseInt(date[1]),
Integer.parseInt(date[0])
);
} else if (data.indexOf("-") > -1) {
String[] date = data.split("-");
if (date.length != 3) {
throw new IllegalArgumentException("Data inválida");
}
localDate = LocalDate.of(
Integer.parseInt(date[0]),
Integer.parseInt(date[1]),
Integer.parseInt(date[2])
);
} else {
throw new IllegalArgumentException("Data inválida");
}
this.valor = valor;
this.quantidade = quantidade;
this.data = localDate;
}
public Negociacao(double valor, double quantidade, LocalDate data) {
this.valor = valor;
this.quantidade = quantidade;
this.data = data;
}
/**
* @return O valor da negociação
*/
public double getValor() {
return valor;
}
/**
* @return A quantidade negociada
*/
public double getQuantidade() {
return quantidade;
}
/**
* @return A data da negociação
*/
public LocalDate getData() {
return data;
}
public double getVolume() {
return quantidade * valor;
}
}