Short introduction to REST

REST is a simple way to organize interactions between independent systems.

REST is a simple way to organize interactions between independent systems. It’s been growing in popularity since 2005. This is due to the fact that REST allows you to interact with minimal overhead with clients as diverse as mobile phones and other websites. In theory, REST is not tied to the web, but it’s almost always implemented as such, and was inspired by HTTP. As a result, REST can be used wherever HTTP can.

REST stands for Representational State Transfer. (It is sometimes spelled “ReST”.) It relies on a stateless, client-server, cacheable communications protocol — and in virtually all cases, the HTTP protocol is used.

REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.).

  • Despite being simple, REST is fully-featured; there’s basically nothing you can do in Web Services that can’t be done with a RESTful architecture.

REST is not a “standard”. There will never be a W3C recommendation for REST, for example. And while there are REST programming frameworks, working with REST is so simple that you can often “roll your own” with standard library features in languages like Perl, Java, or C#.

While REST is not a standard, it does use standards:

  • HTTP
  • URL
  • XML/HTML/GIF/JPEG/etc (Resource Representations)
  • text/xml, text/html, image/gif, image/jpeg, etc (MIME Types)

Let’s take a simple web service as an example: querying a phonesnumbersbook application for the details of a given user. All we have is the user’s ID.

Using Web Services and SOAP, the request would look something like this:

 

(The details are not important; this is just an example.) The entire request body now has to be sent (using an HTTP POST request) to the server. The result is probably an XML file, but it will be embedded, as the “payload”, inside a SOAP response envelope.

And with REST? The query will probably look like this:

Note that this isn’t the request body — it’s just a URL. This URL is sent to the server using a simpler GET request, and the HTTP reply is the raw result data — not embedded inside anything, just the data you need in a way you can directly use.

  • It’s easy to see why Web Services are often used with libraries that create the SOAP/HTTP request and send it over, and then parse the SOAP response.
  • With REST, a simple network connection is all you need. You can even test the API directly, using your browser.

 

HTTP Request Methods: GET and POST

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.

HTTP works as a request-response protocol between a client and server.

A web browser may be the client, and an application on a computer that hosts a web site may be the server.

HTTP Methods:

Two commonly used methods for a request-response between a client and server are: GET and POST.

The GET method

The GET method is used to retrieve information from a specified URI and is assumed to be a safe, repeatable operation by browsers, caches and other HTTP aware components. This means that the operation must have no side effects and GET requests can be re-issued without worrying about the consequences.

One downside of GET requests is that they can only supply data in the form of parameters encoded in the URI (known as a Query String) or as cookies in the cookie request header. Therefore, GET cannot be used for uploading files or other operations that require large amounts of data to be sent to the server.

The POST method

The POST method is used for operations that have side effects and cannot be safely repeated. For example, transferring money from one bank account to another has side effects and should not be repeated without explicit approval by the user.

The POST request message has a content body that is normally used to send parameters and data. Unlike using the request URI or cookies, there is no upper limit on the amount of data that can be sent and POST must be used if files or other variable length data has to be sent to the server.

Compare GET vs. POST

The following table compares the two HTTP methods: GET and POST.

GET POST
BACK button/Reload Harmless Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
Encoding type application/x-www-form-urlencoded application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
History Parameters remain in browser history Parameters are not saved in browser history
Restrictions on data length Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) No restrictions
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also allowed
Security GET is less secure compared to POST because data sent is part of the URL

Never use GET when sending passwords or other sensitive information!

POST is a little safer than GET because the parameters are not stored in browser history or in web server logs
Visibility Data is visible to everyone in the URL Data is not displayed in the URL

Other HTTP Request Methods

The following table lists some other HTTP request methods:

Method Description
HEAD Same as GET but returns only HTTP headers and no document body
PUT Uploads a representation of the specified URI
DELETE Deletes the specified resource
OPTIONS Returns the HTTP methods that the server supports
CONNECT Converts the request connection to a transparent TCP/IP tunnel

Installing and starting the H2 database on Ubuntu/Linux Mint

It is a very simple and straightforward task. Download the H2 zip file (for example Version 1.3.176 (2014-04-05), Last Stable).
Open a terminal window
And then run:

This should start the H2 server tool and open a browser window that lets you connect to a database.

H2 server tool

 

A bit of typing, few clicks and it’s installed and running.

What is JPA? And what its interrelation with Hibernate?

JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database, meaning it provides guidelines for developing an interface that complies with a certain standard. While JPA dictates an interface, it does not provide an implementation of that interface, meaning there is no underlying code that performs the operations to persist an object to a relational database.

Hibernate ORM (Hibernate in short) is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions. Hibernate’s primary feature is mapping from Java classes to database tables; and mapping from Java data types to SQL data types. Hibernate also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from manual handling and object conversion of the result set.

Ok, that’s the definition for both, but what is the relationship between them?

Hibernate is the actual implementation of JPA specification. Simply imagine the JPA as an interface.

And Hibernate as a class that implement JPA interface.

Hibernate is a JPA provider. When you use Hibernate with JPA you are actually using the Hibernate JPA implementation. The benefit of this is that you can swap out Hibernate’s implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate (hibernateSelect() from my example) you are locking into the implementation because other ORMs (Eclipse Link, OpenJPA, DataNucleus) may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.

To recap. JPA is not an implementation, it will not provide any functionality within your application. Its purpose is to provide a set of guidelines that can be followed by JPA providers to create an ORM implementation in a standardized manner. This allows the underlying JPA implementation to be swapped and for developers to easily transition (think knowledge wise) from one implementation to another.

How to install Tomcat Server 7 on Linux Mint 17.3?

Apache Tomcat™ is an open source software implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. For more details about you can visit apache official site http://tomcat.apache.org/ .

1. Check your JAVA installation

Java is mandatory to run Apache Tomcat, Use the below command to check whether Java is installed on your system or not.

If no, Install OpenJDK or Oracle Java before proceeding Tomcat Installation.

2. Downloading Apache Tomcat Archive file.

Use the this Tomcat 7 link to download the latest stable release of Apache Tomcat 7 Archive file or use the following command to download Tomcat 7.0.68 from Apache server.

After completed download please extract this archive file in /opt directory. Of course you may change this location depending on your setup.

3. Setup Environment Variable.

Before starting Tomcat, Configure environment variables by adding entry in ~/.bashrc file, So that system environment can set on system boot up with the following command.

4. Start Tomcat.

5. Access Apache Tomcat.>

Open your web browser and navigate to http://localhost:8080 or http://your-server-ip-address:8080 or http://127.0.0.1:8080 By default tomcat serving port number 8080 (make sure no other app is running using this port).

6. Setup User Accounts.

If you want to access Server Status, Manager App & Host Manager in tomcat, you need to add manager-gui and admin-gui rols under conf/tomcat-users.xml file.

  • manager-gui : Control Server Status & Manager App
  • admin-gui : Control Host Manager

You can access those things by adding the UserName and password to manager-gui and admin-gui rols (Use your username & password instead of us). Stop tomcat service and add user and roles to your conf/tomcat-users.xml file under tomcat-users then save + exit and start tomcat that’s it.

7. Create Tomcat7 Init Script

Create a init file /etc/init.d/tomcat7 using following content. Below you will find my init file for the user maro:

Now execute following commands to set proper permissions and symbolic links for init script.

And it’s DONE!

 

Difference Between Abstract Class and Interface in Java

A short comparison of an abstract class and interface in Java.

 Abstract Classes  Interfaces
 abstract class can have static, final or static final variable with any  access specifier  interface can have only static final (constant) variable i.e. by default
 abstract class can extend from a class or from an abstract class  interface can extend only from an interface
 in abstract class keyword ‘abstract’ is mandatory to declare a method  as an abstract  in an interface keyword ‘abstract’ is optional to declare a method as  an abstract
 abstract class can have both abstract and concrete methods  interface can have only abstract methods
 abstract class can have protected , public and public abstract methods  interface can have only public abstract methods i.e. by default
 a class can extend only one abstract class  a class can implement any number of interfaces
 abstract class can extend only one class or one abstract class at a time  interface can extend any number of interfaces at a time

Tricky Java question

What is the output of the below program?

My first guess was that method with String parameter will be called and I was right, I don’t know, somehow I couldn’t think about null as a Object. But as a tricky question it would be too simple just to say that second method will called and that’s it.
After some additional reading I found proper explanation for it, it’s a quite a lot to read Method Invocation Expressions , in short:
In case of overloading, compiler picks the most specific function. Obviously String class is more specific that Object class.

I’ve never encounter this kind of ‘problem’ when coding, but I can imagine some surprised developers tracing their code.