SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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
15.
Reflection
Code generation
lördag 24 januari 15
16.
public static interface ContactMapper extends
ObjectMapper<Contact> {}
public Contact parseContact(String string) {
ContactMapper mapper = GWT.create(ContactMapper.class);
Contact contact = mapper.read(jsonString);
return contact;
}
gwt-jackson
lördag 24 januari 15
17.
Good
• Minimal boiler plate
• Can share code
between server and
client
Bad
• Only creating and
reading strings
Jackson
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 files
• Optimized format
GWT-RPC
lördag 24 januari 15
24.
GET /contacts
DELETE /contacts/5
PUT /contacts { name: "John Doe", ... }
REST
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
27.
Good
• Any server
• Any client
Bad
• Some boilerplate
resty-gwt
lördag 24 januari 15
30.
Send interfaces
instead of objects
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
32.
New possibilities
Send partial updates
Manage entity identity
Use instance methods for RPC
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
34.
Define message
Generate code
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
42.
What if we
completely hide
the transport?
lördag 24 januari 15
43.
// Fire event
@Inject
Event<Contact> contactEvent;
public void updateContact(Contact contact) {
contactEvent.fire(contact);
}
// Listen to event
public void contactUpdateObserver(@Observes Contact contact) {
ContactDAO.updateContact(contact);
}
CDI events
lördag 24 januari 15
44.
Good
• Transparent
communication
• Server push
Bad
• Transparent
communication
• Tied to the
framework
Errai Bus
lördag 24 januari 15
45.
Questions?
Answers?
Please rate the talk at
gwtcreate.com/agenda
? leif@vaadin.com
lördag 24 januari 15