Rapid java backend and api development for mobile devices

2,449 views

Published on

Java Saturday, Ciklum Odessa, Oct 29-2011

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,449
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Rapid java backend and api development for mobile devices

  1. 1. Yaroslav Pogrebnyak Software Developer, Ciklum [email_address] www.pogrebnyak.info Rapid Java Backend & API Development for Mobile Devices
  2. 2. Devices & Apps Hell How to interact? A lot of work for backend developers ]:->
  3. 3. Device ↔ Server interaction
  4. 4. How to define API? HTTP + MediaType + ... = REST? It Depends! - URI - Media-Type: xml, json, etc - Operations: create, update, delete, … - Custom conventions GET http://example.com/users/ POST http://example.com/users/ DELETE http://example.com/users/42
  5. 5. Data Interchange Protocols PlainText, XML, JSON, ProtocolBuffers , BERT, BSON, Thrift, MessagePack, Custom Protocol... 1. Size 2. Performance 3. Usability 4. Platforms
  6. 6. 68 bytes: { &quot;status&quot; : &quot;OK&quot; , &quot;response&quot; : { &quot;id&quot; : 42 } } 5 bytes (hex dump): 0801 102a 0a JSON vs XML vs Protocol Buffers 119 bytes: <? xml version=&quot;1.0&quot;?> < message > < status >OK</ status > < response > < id >42</ id > </ response > </ message >
  7. 7. HTTP POST /api/register/ Content-Type: application/x-protobuf Accept: application/x-protobuf serialize deserialize HTTP 200 OK Content-Type: application/x-protobuf Content-Length: 5 serialize deserialize Protocol Buffers scenario
  8. 8. Implementation
  9. 9. API requires strength , Java-world's advantage: Static Typing
  10. 10. Java + Maven + Spring + Jersey + Protocol Buffers
  11. 11. Create project Jersey Simple Webapp $ mvn archetype:generate -DarchetypeCatalog= http://download.java.net/maven/2 Spring + Jersey $ mvn archetype:generate -DarchetypeCatalog= http://seratch.github.com/mvn-repo/releases DIY $ cd project $ vim pom.xml :) Minimalistic Secret Template :) http://pogrebnyak.info/ciklum/spring_jersey_gpb.zip
  12. 12. Protocol Buffers Schema package myapi ; // POST /api/register message RegisterRequest { required string login = 1; optional string email = 2; } // Response message RegisterResponse { enum Status { SUCCESS = 0; ALREADY_EXISTS = 1; } required Status status = 1; required string id = 2; }
  13. 13. Conventions /some/endpoint Request: message Name Request { … } Response: message Name Response { enum Status { … } required Status status = 1; ... }
  14. 14. Configuration Implement Protocol Buffers Serializer/Deserializer for Jersey @Provider @Component @Consumes ( “application/x-protobuf” ) public class ProtobufMessageReader implements MessageBodyReader<Message> { // ... @Provider @Component @Produces ( “application/x-protobuf” ) public class ProtobufMessageWriter implements MessageBodyWriter<Message> { // ...
  15. 15. applicationContext.xml DataSource & transaction management web.xml Jersey Spring Servlet & Spring context listeners Protofile & package for generated files Controller classes Services & dao Other Stuff Protobuf serializers pom.xml project configuration
  16. 16. Jersey Сontroller Example @Component @Transactional @Path ( &quot;/api&quot; ) public class ApiController { @Autowired private ClientUserService userService; @POST @Path ( &quot;/register&quot; ) public RegisterResponse register( RegisterRequest r) { User user = new User(r.getLogin()); userService.createUser(user); return RegisterResponse . newBuilder () .setStatus(Status.Success) .setId(user.getId()) .build(); } }
  17. 17. What Else? <ul><li>Authentication </li></ul><ul><li>API throttling </li></ul><ul><li>Caching & distributing </li></ul><ul><li>Testing </li></ul><ul><li>Error handling </li></ul><ul><li>Stateful API? </li></ul>
  18. 18. Thank you! Yaroslav Pogrebnyak [email_address] www.pogrebnyak.info Presentation: http://pogrebnyak.info/ciklum/spring_jersey_gpb.ppt Sample project: http://pogrebnyak.info/ciklum/spring_jersey_gpb.zip

×