Thursday, January 27, 2011

Restful webservice Post Client

Restful webservice Post Client

1. Netbeans 6.9
2. Create a project Helloworld
3. Deploy and test the service
4. Right Click on project properties set relative url as resources/helloworld

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBElement;
import javax.ws.rs.FormParam;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.List;

/**
 * REST Web Service
 *
 * @author localadmin
 */

@Path("helloworld")
public class HelloWorld {
    @Context
    private UriInfo context;

    /** Creates a new instance of HelloWorld */
    public HelloWorld() {
    }

    /**
     * Retrieves representation of an instance of helloworld.HelloWorld
     * @return an instance of java.lang.String
     */
    @POST
   // @Produces("text/html")
   @Produces("application/xml")
  // @Consumes("application/x-www-form-urlencoded")
 //   public List<userData> getHtml(@QueryParam("name") String user_id) {
      public List<userData> getHtml(@FormParam("test") String user_id) {
           userData toReturn = new userData();
                toReturn.name = user_id;
        List<userData> retUser = new ArrayList<userData>();
        retUser.add(toReturn);
   return retUser;
  }

    /**
     * PUT method for updating or creating an instance of HelloWorld
     * @param content representation for the resource
     * @return an HTTP respons with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/html")
    public void putHtml(String content) {
    }
}


2nd Class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class userData {
    public String name;
  
}



WebService Post Client

<html>
<head>
<script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
type="text/javascript">
</script>

    <script type="text/javascript">
      function register(){
    //    window.open("/welcome.html",'welcome','width=300,height=200,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
        alert("hey");
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/HelloWorld/resources/helloworld",
            data: "user_id=" + document.getElementById("user_id").value ,
            success: function(msg){
                            window.alert(msg);
                        },
                error: function(xmlHttpRequest, status, err) {
                    alert('Status ' + status + ', Error ' + err);
                }
    });
        }
    </script>
</head>
<body>
<form action="http://localhost:8080/HelloWorld/resources/helloworld" method="post">
<textarea name="test" rows="8" cols="40" value="tangeo"></textarea>
<input type="text" id="name" name="name" value="harshal"/>
<input type="submit" value="submit"/>
<div id="response">
hello
    </div>
</form>
</body>
</html>


Create the security key and password

1 ./usr/lib/jvm/java-6-sun/bin# keytool -genkey -alias tomcat -keypass changeit -keystore sslkey.bin -storepass changeit
2. Go to glass fish Server http://localhost:4848 Network Config http listener 2 enable ssl and set port number to 8443.
3. Click on tab SSL and put in certificate name as tomcat or the one given above as alias.
4. In Key Store place /usr/lib/jvm/java-6-sun/bin/sslkey.bin
5. Go back to HelloWorld project in netbeans and click on web.xml then click on security tab. Click on Add Web Resource Collection
 put the name as http://localhost:8080/HelloWorld/resources/application.wadl which is displayed when you right click project in netbean and Test WebService on this page the resource name is present.
6. Coming back after web resource collection in url pattern place /helloworld.

No comments:

Post a Comment