jmcx / restlet-contacts-app

A simple contact list app built with Restlet
0 stars 0 forks source link

Using Restlet to serialise and deserialise JSON #3

Open jmcx opened 10 years ago

jmcx commented 10 years ago

Use Restlet's ability to handle representation serialisation and deserialisation to / from JSON.

templth commented 10 years ago

Hi Jonathan,

I think that you can go further by levaring the converter support of Restlet. This allows directly using beans instead of representations within the annotated methods of server resources :

@Get
public Contact loadContact() {
    (...)
}

@Put
public Contact storeContact(Contact contact) {
    (...)
}

The Contact bean is your Contact class (POJO).

The content creation is delegated to registered converters. Adding extension jars in the classpath can automatically register such converters. It's the case of the Jackson extension of Restlet (org.restlet.ext.jackson) for Jackson2. Don't forget to add Jackson2 jar files in your classpath.

You can notice that such mechanism is also usable on the client side using annotated interfaces of Restlet :

ClientResource cr = new ClientResource("...");
ContactService service = cr.wrap(ContactService.class);
Contact contact = service.loadContact();

The ContactService interface could be something like that :

public interface ContactService {
    @Get
    Contact loadContact();
    @Put
    Contact storeContact(Contact contact);
}