Register custom converters to deserialization

22 views
Skip to first unread message

RafaelViana

unread,
Feb 14, 2011, 7:38:21 AM2/14/11
to restfulie-java
When I add custom converters in the VRaptor server. I also need to add
these converters to the restfulie client to avoid
CannotResolveClassException

It could be done a method like withConverters(...) to add custom
converters, but the converters don't have a common interface.

I'm using the method configure to pass a custom xstream instance to
the XmlMediaType.

public XmlMediaType configure(XStream xstream) {
this.xstream = xstream;
return this;
}

However, using the method above. This XStreamHelper won't be called:

private XStream getXstream(RestClient client) {
if (xstream == null) {
this.xstream = helper.getXStream(getTypesToEnhance(),
getCollectionNames(client));
configure(xstream);
}
return xstream;
}

What is the utility of this XStreamHelper? Is it used to hypermedia?

--------

Another question: I saw the helper process the types given in
withTypes(...).

But, I'm with a strange situation.

Case 1:
restfulie.getMediaTypes().register(new
XmlMediaType().withTypes(Produto.class));
response.getResource();
Output:
CannotResolveClassException

Case 2:
XStream xstream = new XStream();
xstream.processAnnotations(Produto.class);
restfulie.getMediaTypes().register(new
XmlMediaType().withTypes(Produto.class).configure(xstream));
response.getResource();

Output:
List serialized.

I'll try to reproduce this "error" in a separated project.

Lucas Cavalcanti

unread,
Feb 14, 2011, 7:50:29 AM2/14/11
to restful...@googlegroups.com
Theoretically, when you use
restfulie.getMediaTypes().register(new XmlMediaType().withTypes(Produto.class));

it should call xstream.processAnnotations(Produto.class);

And yes, it adds hypermedia capabilities to the Produto class serialization.

if you want to add custom converters, you should subclass the mediaType:

restfulie.getMediaTypes().register(new XmlMediaType() {
     @Override
     protected void configure(XStream xstream) {
         xstream.registerConverter(new MyCustomConverter());
     }
});

If it is possible, use the first approach.

What is the error message of the CannotResolveClassException?

[]'s
Reply all
Reply to author
Forward
0 new messages