RSS

Monthly Archives: March 2014

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

 
22 Comments

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.

 
5 Comments

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.

 
2 Comments

Posted by on March 10, 2014 in java

 

Tags: ,

 
%d bloggers like this: