Wednesday, 29 April 2015

Redirect In Jersey


How to  redirect to any url in jersey


  @GET
  @Path("/user")
    public Response userVerification(@Context Response response){
      
      java.net.URI location = new java.net.URI("http://www.ariseter.com");
   

      return Response.temporaryRedirect(location).build();

    }



This is redirect to www.ariseter.com

Thursday, 23 April 2015

How to Install Mysql in Ubuntu

Installing Mysql In Ubuntu

 follow these steps:
Step 1:

sudo apt-get update









Step 2:

sudo apt-get install mysql-server-5.6

How to Install Java In Ubuntu

Installing default JRE/JDK

 Step 1:
  
       sudo apt-get update
Step 2:
      sudo apt-get install default-jre  
Step 3:
  sudo apt-get install default-jdk
   
Step 4:

 java -version


Installing OpenJDK 7

 Step 1:

sudo apt-get install openjdk-7-jre

Step 2:

sudo apt-get install openjdk-7-jdk

 

 

 


 

 

 

 

Friday, 17 April 2015

StringUtils.isNotBlank() VS StringUtils.isNotEmpty() in Java

  StringUtils.isNotBlank() VS StringUtils.isNotEmpty() in Java



isNotBlank



public static boolean isNotBlank(String str)

    Checks if a String is not empty (""), not null and not whitespace only.

    

    StringUtils.isNotBlank(null) = false

    StringUtils.isNotBlank("") = false

     StringUtils.isNotBlank(" ") = false

     StringUtils.isNotBlank("java") = true

    StringUtils.isNotBlank("java  ") = true

    

    Parameters:

    str - the String to check

    Returns:true if the String is not empty and not null and not whitespace
        
        
        



isNotEmpty

public static boolean isNotEmpty(String str)

    Checks if a String is not empty ("") and not null.

     StringUtils.isNotEmpty(null)      = false
     StringUtils.isNotEmpty("")        = false
     StringUtils.isNotEmpty(" ")       = true
     StringUtils.isNotEmpty("java")     = true
     StringUtils.isNotEmpty("java  ") = true
    




    Parameters:
        str - the String to check, may be null

    Returns:
        true if the String is not empty and not null



Related Post:





Monday, 13 April 2015

RENAME Table AND Column Name In Mysql Database


Rename Table Name In Mysql:

RENAME TABLE  old_name_of_table To new_name_of_table;

Example:

RENAME TABLE user12 To user;


Rename Column Name IN Mysql

ALTER TABLE Table_name CHANGE Old_column_name New_column_name DataType

Example:


ALTER TABLE user CHANGE name user_name varchar(100);

Sunday, 12 April 2015

WHAT IS OPEN SOURCE SOFTWARE ?

Most software products that we buy and use could be called closed source software. The source code of this software cannot be adjusted. We do not have access to the source code; what we buy is compiled code. For example, we cannot modify the hyphenation algorithm of Microsoft Word. This code was written by a Microsoft programmer somewhere in Seattle and cannot be changed; it is blocked for everyone. When you want to change something, you have to pass on your demands to Microsoft.
The opposite applies to the source code of open source software. Open source
code can actually be modified because the vendor includes the source code.  When you think that you can improve Software or extend its functionality, you go ahead and try. You try to find the part in the source code that you want to improve and apply the desired changes. Next you compile and link the existing code to the code that you just wrote, and you have created an improved version. In short, the source code is open and accessible to you. You can even go further. When you think your improved code is really good and useful, you can send it to the vendor of the open source software product. The developers then decide whether they want to add your code to the standard code.
           If they do, others can enjoy your work in the future. If they don’t, you can become such a vendor yourself, as long as you provide your new source code publicly. So either way, an open source license ensures that open source software is improved and is spread into the world.
          In short, open source software. That is easy to understand. Most open source software is also free to use. However, when we talk about selling software that includes open source software, it becomes a different
story .
 Many Open Source Software Have their General Public License .It depends on Software policy.

Saturday, 11 April 2015

HeaderParam Annotation In Jersey


How to Get Value From Header in Jersey

If we want to send any information in header request then we get value of header by using HeaderParam annotation.There is a example how to use @HeaderParam Annotation in jesery


import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

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

    @POST
    public Response addUser(@HeaderParam("token") String token) {
        System.out.println("token=" + token);
        return Response.status(Status.OK).entity(token).build();
    }

}

QueryParam Annotation In Jersey



 @QueryParam Annotation In  Jersey

Example:


http://localhost:8080/JerseyDemo/rest/user/12?p=10


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

    @GET
    @Path("/{id}")
    public Response addUser(@PathParam("id") int id,@QueryParam("p") int page) {
        System.out.println("id=" + id+"page="+page);
        String str = "id is=" + id+"page="+page;
        return Response.status(Status.OK).entity(str).build();
    }

}

Output:

id is=12page=10

PathParam Annotation In Jersey


 @pathparam  Annotation



Binds the value of a URI template parameter or a path segment containing the template parameter to a resource method parameter, resource class field, or resource class bean property. 

 Example :

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

Where "27" is the id of the user. So, in the code, I specified like this

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

    @GET
    @Path("/{id}")
    public Response addUser(@PathParam("id") int id) {
        System.out.println("id=" + id);
        String str = "id is=" + id;
        return Response.status(Status.OK).entity(str).build();
    }

}




Friday, 10 April 2015

RESTful Web Services with Java (JAX-RS) using DELETE Method


RESTful Web Services Using DELETE Method Using Jersey
 
If we are use DELETE method then need a client to run application . you have any client then ok.if you have not any idea about client then you first learn about client. There are many client postman,rest console etc. But I have use rest console client , for rest console client tutorial click here

So now i consider that you can use rest console client .

now follow these step for make a rest application using put method

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

UserHandler.java

package com.javaproficiency.jerseydemo.demo;


import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

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

    /*

     Use @DELETE annotation here

        */
  
    @DELETE
    public Response getMessage(){
        String message="Jersey Hello By Java Proficiency";
        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

   

                               


Output:










RESTful Web Services with Java (JAX-RS) parse JSON (Jersey)


This example parse JSON in RESTful Web Services with  Java Using Jersey.
There are many way for parse JSON in restful web services. In this example  I have used jackson dependency(maven project) if you are not use maven project then you can use jackson jar for JSON parsing.


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

UserHander.java

package com.javaproficiency.jerseydemo.demo;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.javaproficiency.jerseydemo.model.User;

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

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addUser(User user) {
        System.out.println("user name"+user.getName());
        System.out.println("user city="+user.getCity());
        return Response.status(Status.OK).entity(user).build();
    }

}


Step 2:


User.java


package com.javaproficiency.jerseydemo.model;

public class User {

    private String name;
    private String city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}



Step 3: 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 4: 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>InstagramPost</groupId>
    <artifactId>InstagramPost</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>com.craterzone.instagrampost</groupId>
                <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>
            <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
            <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
   
           <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.10.1</version>
        </dependency>
       
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.2</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 5:   Project Structure

                 


Step 6: Hit web service using rest console

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

json:

          {
                "name": "raja",
                 "city": "delhi"
          }





 Response:
                   {
                "name": "raja",
                 "city": "delhi"
                 }







Thursday, 9 April 2015

RESTful Web Services with Java (JAX-RS) using POST Method


RESTful Web Services Using POST Method Using Jersey
 
If we are use POST method then need a client to run application . you have any client then ok.if you have not any idea about client then you first learn about client. There are many client postman,rest console etc. But I have use rest console client , for rest console client tutorial click here

So now i consider that you can use rest console client .

now follow these step for make a rest application using put method

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

UserHandler.java

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

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

/*
 Use @POST annotation here
    */
    @POST
    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

   

                               


Output:










Wednesday, 8 April 2015

How To get Default Character set of Database And Table in mysql


 If you want to know your mysql database default character set then use this
 query

SELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA S WHERE schema_name="DATABASE_NAME";

Output:

+----------------------------+
| DEFAULT_CHARACTER_SET_NAME |
+----------------------------+
| latin1                     |
+----------------------------+


Get Table database default character set using this query

 SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
    information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "DATABASE_NAME" AND T.table_name = "TABLE_NAME";


Output:

+--------------------+
| character_set_name |
+--------------------+
| latin1             |
+--------------------+


Tuesday, 7 April 2015

mysql tutorial for beginners

Linux Tutorial for Beginners



Importance command for Java developer

Basic commands
How to create file.
How to create folder.
how to change permissions.
how to use vi editor.
grep commands.
how to install java on ubuntu using command
command for tomcat
sudo command
pipelines
filter
wildcards.

 Software Instalation

Adobe reader
Mysql
ellipse

Sunday, 5 April 2015

Rest Console Tutorial for Beginners

How to install rest console and use it.


Step 1.
               Go to  Chrome Web Store and search rest console.

                 





Step 2.  Now search result for rest console and  Click on free button in front of rest console and add it .





Step 3. Open rest console


 


Step 4. Now  Request URL  Enter In Request URI  . I use URL= www.ariseter.com

You can also use it www.ariseter.com and Method GET









Step 5. Now Click On GET Button ,You will get result as








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


RESTful Web Services Using PUT Method Using Jersey
 
If we are use PUT method then need a client to run application . you have any client then ok.if you have not any idea about client then you first learn about client. There are many client postman,rest console etc. But I have use rest console client , for rest console client tutorial click here

So now i consider that you can use rest console client .

now follow these step for make a rest application using put method

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

UserHandler.java

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

@Path("/user")
public class UserHander {
 
/*
 Use @PUT annotation here
    */
    @PUT
    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

   

                               


Output:










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