Posts

Showing posts with the label REST

Rest API Design Reference

REST stands for Representational State Transfer. It is an architectural style for developing software. Architectural Constraints REST defines 6 architectural constraints which make any web service – a true RESTful API. Uniform interface Client–server Stateless Cacheable Layered system Code on demand Uniform interface Uniform interface suggests that the resource in a system to have only one logical URI and  Then provide additional information to retrieve the related data. For instance we can provide Links for related resources like child objects, parent objects etc. User of one API end point should be able to identify and work similarly with other Resources. Also, the resource representations across system should follow certain guidelines such as naming conventions, link formats or data format, filter formats pagination formats etc. All resources should be accessible in similar way and should use similar status codes. Client–server Server should not depend on p...

REST API using Play! Framework in JSON and XML Format

Play Framework  Play is heavily inspired by Ruby on Rails . Play supports both Scala and Java and enables developers to develop application faster by following convention over configurations. The following post describes how we can expose REST API Endpoints very quickly supporting multiple format like JSON XML etc using Java with Play Framework. Rest End Point Example  Say We want the List of Users to be exposed as a JSON Response with an endpoint. We will achieve the same with the following steps. Step 1: Define the Routes and Controller Mapping Step 2: Define Entity(Model Object) Step 3: Define the Controller Step 4: Start the Play Application Step1: Define the  routes (/conf/routes) Open the /conf/routes file and add the following line(s). GET /api/v1/User/details/all controllers.MyApplicationApiV1.getUserList() GET /api/v1/User/details/{userId} controllers.MyApplicationApiV1.getUserDetails(userId: Long) These entries in the routes file s...

JAX-RS Jersey Client Example With Post

Let's take example of the operateXMLNumbers restful service, which excepts data in XML format and also returns data in XML format. This service is simple and add two numbers and returns the data. Following show's the service method written using Jersey. Notice that the parameter is mapped object type MathRequest. The Full code can be found at google code. Service Method @POST @Path("operateXMLNumbers") @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) public MathResponse addTwoXMLNumbers(MathRequest rq) { String numa = rq.getOperandA().toString(); String numb = rq.getOperandB().toString(); String operator1 = rq.getOperationTypeNew(); Integer result = doCalculateCoreLogic(operator1, numa, numb); MathResponse rs = new MathResponse(); rs.setMathOperationRQ(rq); rs.setResult(result); return rs; } Client private static void callTwoXMLNumbers() { Client client = Client.create(); We...