REST and Software Architecture
HTTP and REST
REST Principles Detail
JAX-RS Intro
Example: Web Store (JAX-RS)
REST's client–server separation of concerns simplifies component implementation, reduces the complexity of connector semantics, improves the effectiveness of performance tuning, and increases the scalability of pure server components. Layered system constraints allow intermediaries - proxies, gateways, and firewalls - to be introduced at various points in the communication without changing the interfaces between components, thus allowing them to assist in communication translation or improve performance via large-scale, shared caching. REST enables intermediate processing by constraining messages to be self-descriptive: interaction is stateless between requests, standard methods and media types are used to indicate semantics and exchange information, and responses explicitly indicate cacheability.
public class Customer {
private String firstName;
// ...
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
// ...
}
@Path("/customers")
public class CustomerResource {
// ...
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is) {
// ...
}
}
@Path("/customers")
class A {
@POST
@Path("{id}")
@Consumes("application/xml")
public Response foo(@PathParam("id") int id, InputStream is) {
// ...
}
}
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is) {
// ...
return Response.created(URI.create("/customers/" + c.getId())).build();
}
public interface StreamingOutput {
public void write(OutputStream os)
throws IOException, WebApplicationException;
}
// E.g. GET /customer/{id}
return new StreamingOutput {
@Override
public void write(OutputStream os)
throws IOException, WebApplicationException {
outputCustomer(os,c);
}
}
@ApplicationPath("/api")
public class WebShopServer extends Application {
private Set<Object> singeltons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public WebShopServer() {
singeltons.add(new CustomerResource());
}
@Override
public Set<Class<?>> getClasses() {
return empty;
}
@Override
public Set<Object> getSingletons() {
return singeltons;
}
}
public abstract class Application {
private static final Set<Object> emptySet = Collections.emptySet();
public abstract Set<Class<?>> getClasses();
public Set<Object> getSingletons() {
return emptySet;
}
}
WEB-INF/
web.xml
classes/
Customer.class
...
@Path("customers")
public interface ICustomerResource {
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is);
// ...
}
public class CustomerResource implements ICustomerResource {
public Response createCustomer(InputStream is) {
// ...
}
}
RESTful Java with JAX-RS 2.0, Bill Burke
http://en.wikipedia.org/wiki/Representational_state_transfer
Thank you for your attention!