11. JSONObject json = JSONParser.parseStrict(string).isObject();
contact.setName(json.get("name").isString().stringValue());
contact.setYearOfBirth(
(int) json.get("yearOfBirth").isNumber().doubleValue());
contact.setAddress(
parseAddress(json.get("address").isObject()));
JSONArray emailAddresses =
json.get("emailAddresses").isArray();
for (int i = 0; i < emailAddresses.size(); i++) {
contact.getEmailAddresses().add(
emailAddresses.get(i).isString().stringValue());
}
}
JSONValue parsing
lördag 24 januari 15
12. Good
âą Itâs standard
âą Human readable
format
âą Compact format
Bad
âą Not completely
typesafe
âą Boilerplate
âą Client only
JSONValue
lördag 24 januari 15
14. ObjectMapper mapper = new ObjectMapper();
try {
Contact contact = mapper.readValue(string, Contact.class);
} catch (VariousExceptions e) {
// Do something sensible
}
Jackson on the server
lördag 24 januari 15
19. public interface ContactService {
public void saveContact(Contact contact);
public List<Contact> getContacts();
}
Remote Procedure Call
lördag 24 januari 15
20. public interface ContactService {
public AsyncResult<Void> saveContact(Contact contact);
public AsyncResult<List<Contact>> getContacts();
}
Asynchronous
public interface ContactServiceAsync {
public void saveContact(Contact contact,
AsyncCallback<Void> callback);
public void getContacts(AsyncCallback<List<Contact>>
callback);
}
lördag 24 januari 15
21. Good
âą Simple but powerful
concept
âą The default solution
âą Optimized format
Bad
âą Sending the entire
object graph
âą Polymorphism can
cause huge ïŹles
âą Optimized format
GWT-RPC
lördag 24 januari 15
25. @Path("/contacts")
public interface ContactsService {
@GET
public List<Contact> listContacts();
@Path("/{id}")
@DELETE
public void deleteContact(@PathParam("id") int id);
@PUT
public void createContact(Contact contact);
}
JAX-RS
lördag 24 januari 15
26. @Path("/contacts")
public interface ContactsService extends DirectRestService {
// Same content
}
ContactsService contactsService =
GWT.create(ContactsService.class);
REST.withCallback(myContactListCallback).
call(contactsService).listContacts();
resty-gwt
lördag 24 januari 15
31. public interface Contact {
public void setName(String name);
public String getName();
public void setYearOfBirth(int yearOfBirth);
public int getYearOfBirth();
public void setAddress(Address address);
public Address getAddress();
public void setEmailAddresses(List<String> addresses);
public List<String> getEmailAddresses();
}
Contact interface
lördag 24 januari 15
33. Good
âą Entities do not need
to be GWT
compatible
âą Combines RPC and
entity management
âą Automatic request
handling
Bad
âą Complex setup
âą Heavy coupling with
the server
RequestFactory
lördag 24 januari 15
36. Good
âą Any language
âą Google's data
interchange format
âą Backwards
compatible
Bad
âą Binary format
âą Not very widely
used
Protocol Buffers
lördag 24 januari 15
37. What if we put
the server in
control?
lördag 24 januari 15
39. public class ContactState extends SharedState {
public String name;
@DelegateToWidget
public int yearOfBirth;
}
@OnStateChange("name")
private void updateName() {
doSomethingWithTheName(getState().name);
}
@Override
protected ContactState getState() {
return (ContactState) super.getState();
}
State synchronization
lördag 24 januari 15
40. public interface ContactRpc extends ServerRpc {
public void deleteContact(int id);
}
// Register RPC handler on the server
registerRpc(new ContactRpc() {
@Override
public void deleteContact(int id) {
ContactDAO.deleteById(id);
}
});
// Send RPC from the client
public void sendDelete(int contactId) {
getRpcProxy(ContactRpc.class).deleteContact(contactId);
}
RPC
lördag 24 januari 15
41. Good
âą Stateful server
âą Server push
Bad
âą Stateful server
âą Tied to the
framework
Vaadin Connectors
lördag 24 januari 15