Posts

Showing posts with the label API

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 previ

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 says that map

Converting Java Map to String

Java Collections framework, String manipulation etc is something that we often encounter in Development process. For processing collections (like checking null/empty, Intersection, Disjunction) We do have some of the very use full libraries. Some of the Collection related libraries are Apche Commons Collections and Google  Collections(Guava). Problem Use Case This article explains how to convert a Java Map to String(and vice versa) using different libraries and technique. One way is to use StringBuilder(Or String) and loop though the Map and build the String by applying some sort of separator ( for key:value and entry). Here we have to take care of the null value etc. Without Any Library If we want to convert the map to a String with key value separator and also individual entry seperator in the resulting String, we have to write code for that. For a simple Map, we have to iterate though the map, take care of the null values etc. Following is a sample to get String built

Jakarta Commons HttpClient API Example

The Jakarta Commons HttpClient component provides API's for accessing HTTP Resources. We can download HttpClient from Apache and use it. The general process for using HttpClient consists of a number of steps: Create an instance of HttpClient . Create an instance of one of the methods (GetMethod in this case). The URL to connect to is passed in to the the method constructor. Tell HttpClient to execute the method. Read the response. Release the connection. Deal with the response. The following example shows, how we can use HttpClient for Submiting a form data to a URL. import java.io.IOException; import java.util.MissingResourceException; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; public class FundaHttpClient { public static boolean httpPost(HttpServletRequest request) { //UR