Amigos do fórum,
Continuando a bater cabeça com o VRaptor por aqui :lol: , por gentileza, se possível me ajudem com esse cenário:
tenho este controller
public void all(){
List<Model> models = new ArrayList<Model>();
models.add(new Model(1, "Model 1"));
models.add(new Model(2, "Model 2"));
result.use(json()).from(models).serialize();
}
a saída é o json abaixo,
{"list": [{"id": 1,"name": "Model 1"},{"id": 2,"name": "Model 2"}]}
Ok. Estou tentando desserializar isso para uma lista de objetos, com o seguinte codigo no client,
InputStream stream = ... //requisicao http
XStream xstream = new XStream(new JettisonMappedXmlDriver());
System.out.println(xstream.fromXML(stream));
No entanto, o XStream levanta a seguinte exceção,
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: id : id : id : id
---- Debugging information ----
message : id : id
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : id : id
class : java.util.ArrayList
required-type : java.util.ArrayList
path : /list/list/id
line number : -1
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:71)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:68)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:861)
at br.com.service.main.Main.get(Main.java:51)
at br.com.service.main.Main.main(Main.java:29)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: id : id
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68)...
Está faltando definir alguma configuração do XStream no client, ou preciso alterar o json gerado no servidor? O XStream consegue desserializar coleções/arrays?
Queria testar também a geração do json no VRaptor usando o jackson (http://jackson.codehaus.org/). Bastaria criar um componente que implementa JSONSerialization?
Obrigado!