rapid java backend and api development for mobile devices

18
www.ciklum.net Yaroslav Pogrebnyak Software Developer, Ciklum [email protected] www.pogrebnyak.info Rapid Java Backend & API Development for Mobile Devices

Upload: ciklumods

Post on 13-Jan-2015

1.678 views

Category:

Technology


0 download

DESCRIPTION

Java Saturday, Ciklum Odessa, Oct 29-2011

TRANSCRIPT

Page 1: Rapid java backend and api development for mobile devices

www.ciklum.net

Yaroslav PogrebnyakSoftware Developer, Ciklum

[email protected]

Rapid Java Backend & API Development

for Mobile Devices

Page 2: Rapid java backend and api development for mobile devices

Devices & Apps HellHow to interact?

A lot of work for backend developers ]:->

Page 3: Rapid java backend and api development for mobile devices

Device ↔ Server interaction

Page 4: Rapid java backend and api development for mobile devices

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

Page 5: Rapid java backend and api development for mobile devices

Data Interchange Protocols

PlainText, XML, JSON, ProtocolBuffers,

BERT, BSON, Thrift, MessagePack, Custom Protocol...

1. Size2. Performance3. Usability4. Platforms

Page 6: Rapid java backend and api development for mobile devices

68 bytes:{ "status" : "OK", "response" : { "id" : 42 }}

5 bytes (hex dump):0801 102a 0a

JSON vs XML vs Protocol Buffers

119 bytes:<?xml version="1.0"?><message> <status>OK</status> <response> <id>42</id> </response></message>

Page 7: Rapid java backend and api development for mobile devices

HTTP POST /api/register/Content-Type: application/x-protobufAccept: application/x-protobuf

serialize

deserializeHTTP 200 OKContent-Type: application/x-protobufContent-Length: 5 serialize

deserialize

Protocol Buffers scenario

Page 8: Rapid java backend and api development for mobile devices

Implementation

Page 9: Rapid java backend and api development for mobile devices

API requires strength, Java-world's advantage: Static Typing

Page 10: Rapid java backend and api development for mobile devices

Java + Maven + Spring + Jersey + Protocol Buffers

Page 11: Rapid java backend and api development for mobile devices

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

Page 12: Rapid java backend and api development for mobile devices

Protocol Buffers Schema

package myapi;

// POST /api/registermessage RegisterRequest { required string login = 1; optional string email = 2;}

// Responsemessage RegisterResponse { enum Status { SUCCESS = 0; ALREADY_EXISTS = 1; }

required Status status = 1; required string id = 2;}

Page 13: Rapid java backend and api development for mobile devices

Conventions

/some/endpoint

Request:message NameRequest { …}

Response:message NameResponse { enum Status { … }

required Status status = 1; ...}

Page 14: Rapid java backend and api development for mobile devices

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> { // ...

Page 15: Rapid java backend and api development for mobile devices

applicationContext.xmlDataSource & transaction

management

web.xmlJersey Spring Servlet & Spring context listeners

Protofile & package for generated files

Controller classes

Services & dao

Other Stuff

Protobuf serializers

pom.xml project configuration

Page 16: Rapid java backend and api development for mobile devices

Jersey Сontroller Example

@Component@Transactional@Path("/api")public class ApiController {

@Autowired private ClientUserService userService;

@POST @Path("/register") public RegisterResponse register(RegisterRequest r) {

User user = new User(r.getLogin()); userService.createUser(user); return RegisterResponse.newBuilder()

.setStatus(Status.Success) .setId(user.getId()) .build();

}}

Page 17: Rapid java backend and api development for mobile devices

What Else?

Authentication

API throttling

Caching & distributing

Testing

Error handling

Stateful API?

Page 18: Rapid java backend and api development for mobile devices

Thank you!

Yaroslav Pogrebnyak

[email protected]

Presentation: http://pogrebnyak.info/ciklum/spring_jersey_gpb.pptSample project: http://pogrebnyak.info/ciklum/spring_jersey_gpb.zip