Rapid java backend and api development for mobile devices

Post on 13-Jan-2015

1,673 views 0 download

Tags:

description

Java Saturday, Ciklum Odessa, Oct 29-2011

transcript

www.ciklum.net

Yaroslav PogrebnyakSoftware Developer, Ciklum

yaroslav@pogrebnyak.infowww.pogrebnyak.info

Rapid Java Backend & API Development

for Mobile Devices

Devices & Apps HellHow to interact?

A lot of work for backend developers ]:->

Device ↔ Server interaction

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

Data Interchange Protocols

PlainText, XML, JSON, ProtocolBuffers,

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

1. Size2. Performance3. Usability4. Platforms

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>

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

Implementation

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

Java + Maven + Spring + Jersey + Protocol Buffers

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

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;}

Conventions

/some/endpoint

Request:message NameRequest { …}

Response:message NameResponse { enum Status { … }

required Status status = 1; ...}

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

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

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();

}}

What Else?

Authentication

API throttling

Caching & distributing

Testing

Error handling

Stateful API?

Thank you!

Yaroslav Pogrebnyak

yaroslav@pogrebnyak.infowww.pogrebnyak.info

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