Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, May 30, 2014

REST API development with JAVA

As we all know java is a powerful programming language. It offers almost everything for web development and REST API development.

There are many ways through which you can develop REST API, but here i am going to share knowledge about most common and easy to use jersey web service APIs.

Jersey is a framework specially designed to develop REST APIs by Oracle.
In this tutorial i am using jersey 2.0 API, which is the latest release of jersey. While there are some API level changes from jersey 1.0 to 2.0 which offers some more functionality in 2.0 but a major change which blocks all the beginner to start their 1st REST API project is the web.xml changes.

Basically jersey servlet container class has been changed in 2.0. In 1.0 jersey jars the servlet container
class was "com.sun.jersey.spi.container.servlet.ServletContainer" while in 2.0 it has been changed to
"org.glassfish.jersey.servlet.ServletContainer".

Many tutorial available online are old enough and generally uses 1.0 jersey version which creates confusion for the new comer.

so if you are using jersey 1.0 jars then your web.xml should look like this(Basic configuration):
"web.xml if using jersey jars version 1.0"
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.ashish.nigam.lovechat</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.ashish.nigam.lovechat</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 
and if you are using jersey 2.0 jars then your web.xml should look like below(Basic configuration):
"Web.xml" when using jersey jars version 2.0"
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>lovechat</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.nigam.ashish.lovechat</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

So lets start with step by step guide to write your first REST API:

1: Create a Dynamic Web Project.
2: Copy all the downloaded jersey jars in WEB-INF folder inside WebContent folder.
3: Edit web.xml file as described above.
4: Go inside your package directory and open .java file, edit it with most common code shared below.
package com.nigam.ashish.lovechat; // replace it with your package name import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; // Plain old Java Object it does not extend as class or implements // an interface // The class registers its methods for the HTTP GET request using the @GET annotation. // Using the @Produces annotation, it defines that it can deliver several MIME types, // text, XML and HTML. // The browser requests per default the HTML MIME type. //Sets the path to base URL + /hello @Path("/hello") public class Hello { // This method is called if TEXT_PLAIN is request @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { return "Hello Jersey, this is plan text"; } // This method is called if XML is request @GET @Produces(MediaType.TEXT_XML) public String sayXMLHello() { return "" + " Hello Jersey, good to have xlm reply" + ""; } //This method is called if XML is request @GET @Produces(MediaType.APPLICATION_JSON) public String sayJSONHello() { return "Hello Jersey, good to have json reply"; } }


5: Now configure your eclipse with tomcat 6 or 7. (preferred is 7).
6: right click on your project and go to RUN AS--> then RUN ON SERVER.

When it launches on eclipse browser or external browser, at the end add /rest/hello to get string returned
as per API request.
/rest/hello is basically two component. one mentioned in web.xml under "url-pattern" and another as class path in your .java class "@Path("/hello")".


Project configuration image:


Sample Screen for REST API testing:



Reference Links:

(Jersey Java Related)

  1. https://jersey.java.net/documentation/latest/deployment.html
  2. http://stackoverflow.com/questions/22022114/org-glassfish-jersey-servlet-servletcontainer-classnotfoundexception
  3. https://jersey.java.net/apidocs/2.0/jersey/org/glassfish/jersey/servlet/ServletContainer.html
  4. https://blogs.oracle.com/japod/entry/jersey_2_0_integrated_into
  5. https://jersey.java.net/download.html
  6. https://jersey.java.net/
  7. http://www.vogella.com/tutorials/REST/article.html
  8. http://www.9lessons.info/2012/09/restful-web-services-api-using-java-and.html
Complete Source Code used in Blog:
  1. https://app.box.com/s/e2szc0crsbayffejbg2a // source code link
Feel free to ask any question regarding this, and subscribe to my blogs for more new coming blogs and deep dive for existing one.


Thank you

Thursday, February 6, 2014

Appcelerator Titanium Module Development for Android

Module Development for Android 

(Using Titanium Version 3.2)



As Appcelerator has already documented well in the android module development part,
So here in this post i am going to write very practical approach which may help you guys for common module initialisation and linking issues.

There are two approaches for module development:

1: Using Terminal and the Eclipse for development and testing.
2: Using Titanium IDE for development, testing and deployment.

Both got their own benefit and drawbacks.

Here i am going to give Module development demo using eclipse, as except module creation in terminal all other things are same.

Terminal Command to create Module:

titanium320.py create --platform=android --type=module --name=<module name> --id=<module.id.reverseDomain> --android=<andorid sdk path>

Explanation:
titanium320.py :  is titanium.py (Titanium SDK version not CLI version) alias in terminal for mac and reference environment variable name in Windows.
name: module name which suits you.
id: module is as per need.
android: Android SDK path. 

Complete Sample command in my machine:
titanium320.py create --platform=android --type=module --name=calc --id=nigam.ashish.calc --android=/Applications/adt_bundle_mac/sdk

This will create a module in the directory you are currently in, in the terminal.

Module Directory structure will look something like this:

The Import the same in Eclipse where after import it should look something like this:


After that just check your build.properties file, it must contain all these properties for proper module build:

titanium.platform=/Users/ashish.nigam/Library/Application Support/Titanium/mobilesdk/osx/3.2.0.GA/android
android.platform=/Applications/adt_bundle_mac/sdk/platforms/android-10
google.apis=/Applications/adt_bundle_mac/sdk/add-ons/addon-google_apis-google-10
android.ndk=/Applications/android_ndk

Now all set, you are ready to build your first module.
Now right click on your build.xml file and go to "Run As" option and click on Ant Build first option.

Result: It will build the module and create module zip in the module source directory, which can be used in titanium application.

Testing on device or emulator:
Right click on your build.xml file and go to "Run As" option and click on Ant Build second option.
it will open a window where on little bit downside scroll, you see a screen like this:


Now you can choose to run on emulator or device:

This will be your screen after selection you can choose both as well but generally not required.

After that hit enter or click ok and it will build your module, create a zip in the module source directory and launch it on device or emulator.

How to use the build zip in Titanium App:

1: Launch Titanium.
2: Go to Help section.
3: Install titanium Module
4: Browse your module zip file path and choose project in which you want to install.
5: Click ok and done, you are ready to use the module in your app.js.

Note: Installing module in SDK Level is not required until module is very generic and will be used in most of the projects.

For Exposing More APIs in your module rather than just simple build, you can now open CalcModule.java file and start adding methods and properties.

Remember to expose methods and properties with proper annotation:

1: @Kroll.module(name="Calc", id="nigam.ashish.calc") //for module class
2: @Kroll.method
3: @Kroll.getProperty
4: @Kroll.setProperty
5: @Kroll.proxy(creatableInModule=CalcModule.class) //for any added proxy class.

There are many more but for exposing methods and properties only these are required.
You can check out lifecycle methods and events for proxy and viewProxy on Appcelerator GitHub Page.

Life cycle methods:

Methods Demo:

View proxy for exposing view:

View or creating custom view:

Passing Parameters to methods:

These are basics about view and proxy which can be seen from github.

Adding 3rd party JARs in module: 

Right Click on your project and click on build path--> configure build path --> and on libraries section add external JAR.

Browse the jar and add it to your project. Import the file in your class and start using them for module development.


Feel free to write your comment on this and any question related to android module development.