Tenho o controller com o seguinte método:
@Get
@Path("/categoria")
public void getCategorias() {
List<Categorias> categorias = dao.findAll();
if (categorias != null && !categorias.isEmpty()) {
result.use(Results.representation()).from(categorias).recursive().serialize();
} else {
result.use(Results.status()).noContent();
}
}
Tenho o seguinte customjsonserialization:
@Component
public class CustomJSONSerialization extends XStreamJSONSerialization {
public CustomJSONSerialization(HttpServletResponse response, TypeNameExtractor extractor,
ProxyInitializer initializer, XStreamBuilder builder) {
super(response, extractor, initializer, builder);
}
@Override
@SuppressWarnings("deprecation")
protected XStream getXStream() {
XStream xstream = super.getXStream();
xstream.registerConverter(new CollectionConverter(xstream.getMapper()) {
@Override
@SuppressWarnings("rawtypes")
public boolean canConvert(Class type) {
return Collection.class.isAssignableFrom(type);
}
});
return xstream;
}
}
e o seguinte teste:
@Test
public void shouldBeAbleToGetAnListOfCategorias2() throws IOException {
String response = restfulie.at("http://localhost:8084/site/categoria")
.accept("application/json").get().getContent();
List<Categorias> categorias = new Gson().fromJson(response, new TypeToken<List<Categorias>>(){}.getType());
System.out.println(response);
Assert.assertNotNull(categorias);
}
minha classe Categorias:
@XStreamAlias("categoria")
public class Categorias implements Serializable {
private Long idcategoria;
private String descricao;
//gets e sets
}
O PROBLEMA:
o json gerado (que é escrito pelo system.out acima) fica assim:
e isso causa o seguinte erro:
IllegalStateException: Excepted BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
com.google.gson.JsonSyntaxException
Alguém sabe como contornar este problema?

