Sunday, 5 April 2015

RESTful Web Services with Java (JAX-RS) using Jersey Example


Rest services with java(JAX-RS) using jersey example tutorial


Step 1: create handle class and define path , method and response.

UserHandler.java

package com.javaproficiency.jerseydemo.demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

@Path("/user")
public class UserHander {

    @GET
    public Response getMessage(){
        String message="Jersey Hello";
        return Response.status(Status.OK).entity(message).build();
    }
   
}

Step  2. configure web.xml file as

web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JerseyDemo</display-name>
    <servlet>
        <servlet-name>RestServices</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.javaproficiency.jerseydemo.demo</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>RestServices</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Step 3. Add dependency in pom.xml file as bellow.
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>JerseyDemo</groupId>
    <artifactId>JerseyDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.13</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.13</version>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <scope>provided</scope>
            <version>2.16</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>2.16</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-servlet</artifactId>
            <version>2.16</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jdk-http</artifactId>
            <version>2.16</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-simple-http</artifactId>
            <version>2.16</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>2.16</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-servlet</artifactId>
            <version>2.16</version>
        </dependency>
    </dependencies>

</project>

Step 4:   Project Structure





 Step 5: Run Project and Hit url

How to decide url:
 
HostName:PortNumber/ProjectName/+Your Decide Path

Example:

http://localhost:8080/JerseyDemo/rest/user

                               

Download Project  JerseyDemo-Project



Saturday, 4 April 2015

Get Roster List From XMPP IM with Smack API in Java Application

package com.javaproficiency.demo;

import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class GETRoster {
    public static void main(String[] args) {
        GETRoster getRoster = new GETRoster();
        XMPPConnection connection = getRoster.Connect();


       // Login User
        getRoster.loginUser(connection);
  
    // Get Roster Of Login User
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry entry : entries) {
            System.out.println("name=" + entry.getName() + "userName="
                    + entry.getUser());
        }
        connection.disconnect();
    }
   
    public void loginUser(XMPPConnection connection){
        try {
            connection.login("userName","Password");
        } catch (XMPPException e) {
           
            e.printStackTrace();
        }
    }

    /**
     *
     * @return XMPP Connection
     */

    public XMPPConnection Connect() {

        ConnectionConfiguration config = new ConnectionConfiguration(
                "localhost", 5222);

        /*
         * ConnectionConfiguration config = new ConnectionConfiguration(
         * "192.163.2.200", 5222);
         */
        XMPPConnection connection = new XMPPConnection(config);
        try {
            connection.connect();
        } catch (XMPPException e) {

            e.printStackTrace();
        }
        return connection;
    }

}

Login to XMPP IM with Smack for Java applications

Maven dependency

<dependency>
            <groupId>jivesoftware</groupId>
            <artifactId>smack</artifactId>
            <version>3.1.0</version>
 </dependency>
<dependency>
            <groupId>jivesoftware</groupId>
            <artifactId>smackx</artifactId>
            <version>3.1.0</version>
   </dependency>


package com.javaproficiency.demo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class LoginSmack {
          public static void main(String[] args) {
              LoginSmack loginSmack=new LoginSmack();
            XMPPConnection connection=loginSmack.Connect();
          
            try {
                connection.login("Username","Password");
            } catch (XMPPException e) {
               
                e.printStackTrace();
            }
            System.out.println("Login Successfully");
        }
         
          /**
           *
           * @return XMPP Connection
           */
         
          public XMPPConnection Connect() {
             
              ConnectionConfiguration config = new ConnectionConfiguration(
                      "localhost", 5222);

              /*
               * ConnectionConfiguration config = new ConnectionConfiguration(
               * "192.163.2.200", 5222);
               */
              XMPPConnection connection = new XMPPConnection(config);
              try {
                  connection.connect();
              } catch (XMPPException e) {
             
                  e.printStackTrace();
              }
              return connection;
          }
         
      
}

Connecting to XMPP IM with Smack for Java applications

We will connect to openfire server using smack api

package com.javaproficiency.demo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class SmackDemo {
    public static void main(String[] args) {
          SmackDemo smackDemo=new  SmackDemo();

          XMPPConnection connection=smackDemo.XMPPConnection();




        
          System.out.println("Connecting to="+connection.getHost()+"  Post:"+connection.getPort());
    }

    public XMPPConnection XMPPConnection() {
        ConnectionConfiguration config = new ConnectionConfiguration(
                "host", "port");

        /*
         * ConnectionConfiguration config = new ConnectionConfiguration(
         * "192.163.2.200", 5222);
         */
        XMPPConnection connection = new XMPPConnection(config);
        try {
            connection.connect();
        } catch (XMPPException e) {
       
            e.printStackTrace();
        }
        return connection;
    }
}

Openfire Server Tutorial

Friday, 3 April 2015

Spring MVC EXample


Video Tutorial : Spring MVC Example 

Step 1: Create a hello controller

HelloController.java

package com.javaproficiency.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring  MVC Hello World");
        return "hello";

    }

}


Step 2: hello.jsp

Video Tutorial : Spring MVC Example

<html>
<body>
    <h1>Message : ${message}</h1>  
</body>
</html>

 Now configure  applicationContext.xml and web.xml


Step 3:        applicationContext.xml



<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <context:annotation-config />

    <context:component-scan base-package="com.javaproficiency.demo" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>



Step 4: web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>
        <listener>
        <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                </listener-class>
    </listener>
   
        <servlet>
        <servlet-name>hellomvc2</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>hellomvc2</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>
  
</web-app>


step 5:  pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>FirstSimpleSpringDemo</groupId>
  <artifactId>FirstSimpleSpringDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>

     </dependencies>

<!--

   <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
    </properties>
   
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>
   -->
</project>


Project Structure:





Step 6: Now run application

http://localhost:8080/FirstSimpleSpringDemo/web/welcome


Video Tutorial : Spring MVC Example



Thursday, 2 April 2015

Basic Unix Commands For Java Developers

1. cd

       cd Directory name

 2. cd ..
 
     go to just one level above parent folder

3.

rm -rf repository/

remove repository folder and subfolder also

4. ps -ef

 show running processes.

5. ps -ef| grep tomcat7

show  tomcat7 's all processes.

6.  ifconfig

 show ip address in ubuntu

7. sudo service tomcat7 status

show tomcat running status

8.

sudo service tomcat7 stop

 stop tomcat7 if it is running state.

9.
sudo kill -9 PID

kill any processe.

10  ls -al 

show all file and folder in working directory.

11 pwd 

12. history

Show terminal history

13. unzip abc.zip

   extract the zip file

14.   tar -zxvf ellipse9.2.2.tar.gz

extract the tar file

15.  mysql -uroot -proot

For use mysql

16.   mysqldump -u root -p database_name > dump_file_name.sql

Create file name