Sunday, November 25, 2012

Maven Client using Google Protocol Buffer Stream VIA JX-RS

  • Create a Maven java project Get Idea From This Link
  • Use Archetype = java_1.6_archetype
  • Look this pom.xml. What dependencies I am using :)
  • See the blow Code to get Google protocol buffer from service get method.
  • We create user object from user.proto .
  • To see how we can compile this file to UserDTO take a look this post.


  • To run this project:
  •  Right Click Project -> Run AS - > Clean
  •  Right Click Project -> Run AS - > Install
  •  Right Click Project -> Run AS - > Build -> exec:java 
To Create/ Setup service See this link Google Protocol Buffer and JX-RS


Google Protocol Buffers, JX-RS & Maven


             1.    Purpose:

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once and then you can use special generated source code to easily write and read your structured data to and from a variety of data streams. Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats. The blow languages support reading/writing data via Google protocol buffers. 

·         C++
·         JAVA
·         Python 

2.    Prerequisite :

·         Create a web service project. I am using maven for web project creation.
·         Here is an example Maven base JX-RS resteasy web service project.
·         After updating the dependencies for spring security we have the latest pom.xml.

3.    Development:

·         First we will create a (.proto) file(s).This File(s) will represent object and we can compile (.proto) file into class (es) of any of above mentioned languages. I am going to choose java for this tutorial.
·         After creating a maven base web project and installing jx-rs web service into that project. I am creating a user.proto file and I will compile it to make UserDTO.java. Server and client side program will use this file to as stream to communicate (read/write data). 
·         To use start add Add dependency Google Protocol Buffers in pom.xml.
 <dependency>  
<groupId>com.google.protobuf</groupId>   
<artifactId>protobuf-java</artifactId>   
<version>2.4.1</version>   
</dependency>
·         Create a file user.proto. You can see the details under the link.
                 option java_outer_classname = "UserDTO";
                 option java_package = "com.maven.work.dto";

                 message User {
                        required string sessionId = 1;
                       optional string name = 2;
                       repeated string authorites = 3;
                 }



  •   To Compile the user.proto file use protoc.exe
  •   Command is : 
                 protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/user.proto 

    •  I am using windows xp and I used this command: 
                   protoc -I=./ --java_out=C:\\ ./user.proto
      •   Now use this User.Java to fill user session id and name. Take a look into service    code
      •   Name of service is  ( /get_user )





Jx-RS and Maven

In this topic I am going to create a java web service application using jx-rs and maven.

  • So first of all we are going to create a Maven web project.
  • Go to this Link For Project Creation: Create Maven Base Web Project.
  • Open pom.xml file add dependencies. Dependencies mean the jars file that project need. For example we need blow jars for our project.

  1. commons-lang-2.3.jar 
  2. jaxb-api-2.1.jar
  3. jaxb-impl-2.1.4.jar
  4. stax-api-1.0-2.jar
  5. jsr311-api-1.1.jar
  6. slf4j-api-1.5.11.jar
  7. slf4j-jdk-1.5.11.jar
  8. wink-common-<version #>.jar
  9. wink-server-<version #>.jar

  • Instead of adding these jars in project and updating with change inversions, we are using maven which will do this work for us on chnage in pom.xml
  • We need few plugins for maven as well to run this project on Apache tomcat
  • So pom file is here pom.xml.
  • Now go to src/main/webapp/WEB_INF/web.xml
  • Add internal servlet and servlet mapping for jx-rs.
  • Register the web service with javax.ws.rs.Application.
  • I am making a file Services.java which is extending javax.ws.rs.Application.It is registering our service class MainServices.clas with rest servlet 

  • Now write a web service in java file. My service file name is MainServices.java.
  • @Path("/mainServices") annotation on top of  Class MainServices is the path to this web service from the Rest servlet (servlet declare in web.xml). 
  • This thing means this /mainServices will append with path of Rest Servlet which is (/service/ => in web.xml).
  • @GET @Path("/hellow_world" )  @Produces(MediaType.APPLICATION_JSON)
 public String helloWorld(){}
  • Method helloWorld() is working as separate service and its path on url is /hellow_world. And this path is append with the path of Class MainServices "/mainServices".
  • So path to this service is    "BASER_URL + /service/mainServices/hellow_world"
  • @GET means the method to access this service is GET.
  • @Produces(MediaType.APPLICATION_JSON) means this is service is returning json.

  • No take a look on a web service (/user_details) which is getting the data from html forms and methods we are using is post.

  • It is consuming (taking input) the form data   Content Type = application/x-www-form-urlencoded
  • @FormParam("userName") String name. The annotation @FormParam is mapping  html form  field = userName to Java variable name.
  • You can access the web service with the blow url http://localhost:8080/maven.work/service/mainServices/user_details

You can get source code from this link Source Code



Maven Web project & Apache Tomcat

In this topic I am going to create a test web project with maven and deploying going to deploy it on tomcat.


  • Open Eclipse go to File then New  -> Other.. -> Maven Project (In my example name of test project is maven.work)

  • Choose  maven archetype web


  • The enter Group id and Artifact id and finish.

  • The go to File again then Other.. -> Server -> Choose server.
  • Apache Tomcat in our example.




  • Right click the project and go to Properties -> Java Build Path -> Libraries.

  • Then Add Library.
  • Add JRE System Library.
  • Then Add Server Runtime. Choose Apache tomcat 6.
  
  • Add Index.jsp in src/webapp/
  • To deploy project right click project and go to Maven -> Update Project.
  • Then Right Click Project and go to Run AS and then install
  • Then Right Click Project and go to Run AS -> Build -> On dialog go to goal field and the go to Goal and the enter tomcat:run.