RSS

REST API Documentation with Swagger


This is going to focus REST API documentation with Swagger. We know that REST APIs are very popular with modern world technology and most of the technology solutions came up with REST Implementations. Developers who are involved with the REST APIs development and they have kind of problem like how to document the REST APIs and how to simply expose those REST APIs to the end users. Swagger is good solution for above explained problem. So lets talk about swagger integration to the JAX-RS API. I think that you are familiar with the JAVA JAX-RS implementations. If not you have to get some knowledge about JAX-RS implementation.

1. You have to add swagger maven artifact to your project.

2. Secondly you have to mentioned the base path on your web.xml. This URL will be used as backend service call from swagger-ui

3. Finally you have to annotate your REST-APIs by swagger annotations. You can get total idea from following code snippets.

4. Then you have to get the swagger-ui and host it on your local tomcat.

5. Now you can simply view and invoke the REST APIs which is developed by you by using the Swagger UI.

test

test2

 
Leave a comment

Posted by on March 25, 2014 in java

 

Tags: , , ,

Pagination with Java Hibernate Criteria


Hibernate is most popular technology in Java development. Most of the real world application they used kind of hibernate technology with fine tuned mechanisms. If you are in java development I hope the following code snippet will be helpful you to make your application faster.

What I’m going to explain in this post is how to load the chunk of data with efficient manner.
Hibernate Criteria helps to do this as we wish. Think about if you have millions of data in your data table and you are going to display the data in your application. Check the following code snippet

You can see in the above example I have used two numbers to query the data from back end. The pageNumber means which page that your are in and the recordesPerPage will use to filter the max record count going to be query. Then the each time fetch the data from database by given range.

This will really make the performance gain rather than loading whole chunk of data.

 
Leave a comment

Posted by on March 13, 2014 in java, mysql

 

Tags: , , ,

How to create simple maven repository on GitHub


Most of the people who are developing simple important projects in java have the following problem.
The problem is how to expose their java libraries as maven artifact.

This is a simple and easy solution for the above matter.

You can easily create the Git repository on GitHub and do the following steps.

First of all tell maven to deploy artifacts to a temporary staging location inside your target directory. Add this to your project pom.xml

<distributionManagement>
    <repository>
        <id>internal.repo</id>
        <name>Temporary Staging Repository</name>
        <url>file://${project.build.directory}/mvn-repo</url>
    </repository>
</distributionManagement>

<build>
 <plugins>
    <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
               <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
        </configuration>
    </plugin>
 </plugins>
</build>

Run mvn clean deploy. You’ll see maven artifact is deployed your maven repository located in target/mvn-repo.

Secondly we have to upload the created artifact to github. In order to do that add your authentication information to ~/.m2/settings.xml so then the github site-maven-plugin can push to github

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>YOUR-GITHUB-USERNAME</username>
      <password>YOUR-GITHUB-PASSWORD</password>
    </server>
  </servers>
</settings>

Please make sure this file has proper security because some one can take your credentials.Then the next step is give the server details to your project by referring the server id created above.

Go to your project pom file and place the following property

<properties>
    <!-- github server corresponds to entry in ~/.m2/settings.xml -->
    <github.global.server>github</github.global.server>
</properties>

The last step is going to upload your locally created artifacts in to the github. In my point of view you better create new branch on your project as mvn-repo and place the following configuration on your project.

<plugin>
         <groupId>com.github.github</groupId>
         <artifactId>site-maven-plugin</artifactId>
         <version>0.8</version>
         <configuration>
              <!-- git commit message -->
              <message>Maven artifacts for ${project.version}</message>
              <!-- disable webpage processing -->
              <noJekyll>true</noJekyll>
              <!-- matches distribution management repository url above -->
              <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
              <!-- remote branch name -->
              <branch>refs/heads/mvn-repo</branch>
              <!-- If you remove this then the old artifact will be removed and new 
               one will replace. But with the merge tag you can just release by changing 
                                                the version -->
              <merge>true</merge>
              <includes>
                <include>**/*</include>
                </includes>
                <!-- github repo name -->
                <repositoryName>sw-protection-client</repositoryName>
                <!-- github username -->
                <repositoryOwner>YOUR-GITHUB-USERNAME</repositoryOwner>
          </configuration>
          <executions>
              <execution>
                    <goals>
                         <goal>site</goal>
                    </goals>
                    <phase>deploy</phase>
              </execution>
          </executions>
</plugin>

Run mvn clean deploy again. You should see maven-deploy-plugin create the files to your local staging repository in the target directory, then site-maven-plugin committing those files and pushing them to the server. Now the artifact is available to public use.

When you need to use your uploaded artifact you have to do the following step. You need to add your repository as follows

<repositories>
    <repository>
        <id>YOUR-PROJECT-NAME-mvn-repo</id>
        <url>https://raw.github.comYOUR-GITHUB-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

Now you can refer your maven artifacts easily. I hope now you can expose your simple artifacts as you wish.

 
Leave a comment

Posted by on March 10, 2014 in java

 

Tags: ,

Learn Builder Pattern through JAVA


1. What is builder pattern?
Builder pattern is one of commonly using design patterns to solve the problems in software development.
2. Where we can use it?
Its very simple. If you need to build the Same type objects with different options you can use the builder pattern. Example – Going to build a Computer, You can have multiple options to build the Computer.
1. Computer 1 – Mother Board, HD, RAM, Power supplier.
2. Computer 2 – Mother Board, HD, RAM, Power supplier, CD ROM, Secondary HD.
3. Computer 3 – Mother Board, HD, RAM, Power supplier, DVD ROM, Keyboard, Mouse.

Lets look at the construction of Computer object without using the builder pattern.

public class Computer {

    private String motherBoard;
    private String ram;
    private String hd;
    private String powerSupplier;

    //Optional fields
    private String cdrom;
    private String secondaryHd;
    private String keyboard;
    private String mouse;

    public Computer(String motherBoard, String ram, String hd, String powersupplier, 
            String cdrom, String secondaryHd, String keyboard, String mouse) {

    }

    public String getMotherBoard() {
        return motherBoard;
    }

    public String getRam() {
        return ram;
    }

    public String getHd() {
        return hd;
    }

    public String getPowerSupplier() {
        return powerSupplier;
    }

    public String getCdrom() {
        return cdrom;
    }

    public String getSecondaryHd() {
        return secondaryHd;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public String getMouse() {
        return mouse;
    }

}

Problems in the above code snippet:
1. When you need to construct the object you have to pass the parameters in correct order in to the constructor. When the parameters count is high there is a chance to get swap the arguments.
2. Even some of the arguments are optional, you have to pass the argument as NULL.
3. One day if you need to add new parameter in to the constructor you have more code changes.

All the above problems are going to be solved with the builder pattern. Lets look at the builder pattern implementation.
1. You need to create static nested class and copy all the arguments to the builder class.
2. The Builder class should have a public constructor with all the required attributes as parameters as well as setters for the optional parameters and return the same builder object.
3. build() method in the builder class that will return the expected Object.

public class Computer {

    
    private String motherBoard;
    private String ram;
    private String hd; 
    private String powerSupplier;

    //Optional fields
    private String cdrom;
    private String dvdrom;
    private String secondaryHd;
    private String keyboard;
    private String mouse;

    private Computer(ComputerBuilder builder) {
        this.motherBoard = builder.motherBoard;
        this.ram = builder.ram;
        this.hd = builder.hd;
        this.powerSupplier = builder.powerSupplier;
        
        this.cdrom = builder.cdrom;
        this.secondaryHd = builder.secondaryHd;
        this.keyboard = builder.keyboard;
        this.mouse = builder.mouse;
    }

    public String getMotherBoard() {
        return motherBoard;
    }

    public String getRam() {
        return ram;
    }

    public String getHd() {
        return hd;
    }

    public String getPowerSupplier() {
        return powerSupplier;
    }

    public String getCdrom() {
        return cdrom;
    }

    public String getSecondaryHd() {
        return secondaryHd;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public String getMouse() {
        return mouse;
    }

    public void setDvdrom(String dvdrom) {
        this.dvdrom = dvdrom;
    }

    public static class ComputerBuilder {

        private String motherBoard;
        private String ram;
        private String hd;
        private String powerSupplier;

        //Optional fields
        private String cdrom;
        private String dvdrom;
        private String secondaryHd;
        private String keyboard;
        private String mouse;

        public ComputerBuilder(String mb, String ram, String hd, String pwSupplier) {
            this.motherBoard = mb;
            this.ram = ram;
            this.hd = hd;
            this.powerSupplier = pwSupplier;
        }
        
        public ComputerBuilder setCDRom(String cdrom){
            this.cdrom = cdrom;
            return this;
        }
        
         public ComputerBuilder setDVDRom(String dvdrom){
            this.dvdrom = dvdrom;
            return this;
        }
        
        public ComputerBuilder setSecondaryHD(String secondaryHd){
            this.secondaryHd = secondaryHd;
            return this;
        }
        
        public ComputerBuilder setKeyboard(String keyboard){
            this.keyboard = keyboard;
            return this;
        }
        
        public ComputerBuilder setMouse(String mouse){
            this.mouse = mouse;
            return this;
        }
        
        public Computer build(){
            return new Computer(this);
        }
        
    }

}

Now build above explained three types of Computers through the builder class as follows.

    public static void main(String[] args) {
        Computer computer1 = new ComputerBuilder("M", "ram", "hd", "pw").build();
        Computer computer2 = new ComputerBuilder("M", "ram", "hd", "pw")
                .setCDRom("cdrom").setSecondaryHD("hd").build();
        Computer computer3 = new ComputerBuilder("M", "ram", "hd", "pw")
                .setDVDRom("dvd").setKeyboard("keyboard").setMouse("mouse").build();
        
    }

Computer class has not contain the public constructor so the only way is to construct the object is through the ComputerBuilder class.

You can download the complete source form here

 
Leave a comment

Posted by on January 10, 2014 in design-paterns, java

 

Tags: , ,

Building a Charts Dashboard with Google Apps Script


This is grate video about creating dashboards on top of google

 
Leave a comment

Posted by on November 18, 2013 in Other

 

Tags: ,

How to run the android application – Installed Android in VirtualBox (Ubuntu 12.04)


I know most software developers having this problem “What is this problem ?” : when you going to run the android application on the emulator which is bind with your IDE(Netbeans, Eclipse) it will take two three minutes to load.

Is there any solution for this? Yes it is, lets look at the solution step by step.

1. I’m using Netbeans as my IDE and I assume that you all set up the android plugin on Netbeans and point the Android SDK in the android section.
ex/ Now you can create the android application with your netbeans IDE.
2. You have to set up the VirtualBox on Ubuntu and install the Android OS on top of that.
you can easily do this step by following this blog post

2. Then you have to point the sdk/tools and sdk/platform-tools

Open bash file

 
sudo gedit ~/.bashrc

Insert the following lines in bottom of the file then save and close.

# Android tools
export PATH=${PATH}:Your_Android_SDK_Path/tools
export PATH=${PATH}:Your_Android_SDK_Path/platform-tools

to source your bash file follow this command.

source ~/.bashrc

3. Now try the following command on your terminal. If there is any connected devices you can see now.

adb devices

4. Now you have setup the adb command perfectly so go to Virtual box setting and see the following network settings are there in your android VM.
vmandroid

5. Start your android VM and Press Alt + F1 to bring up the console in the emulator (IE. VirtualBox window). (Alt + F7 To bring back the Android GUI) and type the “netcfg” command to see the IPs

androidstart1

android1

Press Alt + F1
Screenshot from 2013-10-07 18:31:13

6. Now you know the emulators IP address now you need to go to terminal in your Ubuntu machine and connect the device as follows.

 adb connect 192.168.1.9

and

If it is unable to connect, check your virtual machine’s network settings. Make sure it’s the correct adapter. If that doesn’t fix it refer to the following link: http://www.android-x86.org/documents/debug-howto.

7. Then go to netbeans and run your android project then it will ask the select the emulator to run so now you can see your VM is there.
emu

Now select the VM emulator and run
helloworld

 
Leave a comment

Posted by on October 7, 2013 in android, Android App

 

Tags: , ,

Install LAMP(Apache, Mysql, php) and PhpMyAdmin on Ubuntu 12.04


Most people doing software development on linux and they need to install the LAMP on there machines so these are the steps to achieve above task.

1. First you have to install the “tasksel” on Ubuntu. so go to terminal and follow the command.

sudo apt-get install tasksel

2. Then run the tasksel.

sudo tasksel

3. Select the LAMP server and Install.
lamp
4. Now you need to verify the php whether its working properly. so you can open the following file on terminal

sudo vi /var/www/info.php

Then past the following command on it and save

<?php
 phpinfo();
?>

5. Now restart the server

 sudo /etc/init.d/apache2 restart

6. Now go to browser and visit the http://localhost/info.php page
info.php

7. Now you have completely setup the LAMP server but you don’t have nice management console to access the MySql Database. So that is why we need to install phpMyAdmin console.

8. Follow the steps to install phpMyAdmin

 sudo apt-get install phpmyadmin

apache

passmyadmin

9. Once the setup is completed you can go to the management console

mysqlAdmin

10. Don’t worry if you got Page Not found so then you have to do the extra step to set up phpmyadmin under Apache manually.

Open the file

 sudo gedit /etc/apache2/apache2.conf

Copy past the following line in the bottom of the apache2.conf file

 Include /etc/phpmyadmin/apache.conf

Now restart the server as follows and go to management console

sudo /etc/init.d/apache2 restart
 
1 Comment

Posted by on October 7, 2013 in mysql, php

 

Tags: , , ,

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: