Posts

Showing posts from March, 2014

Pass complex object into RESTful service

Suppose, I have Employee.java class. public class Employee {      private String firstName;      private String lastName;      private String city;     // getter     // setter     // override toString() to print the variables } I want to set values to Employee class through REST service: @POST @Path("/saveemployee") @Consumes(MediaType.APPLICATION_JSON) public Response saveEmployee(Employee employee){     Response response  = null;     System.out.println(employee.toString());     // further procees to store into db     return response; } Now, I want to call this rest service inside my java class: import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.method...

How to call secured RESTful service in java?

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; ... ... String url = "pankaj-lilhare.blogspot";    /* http://pankaj-lilhare.blogspot:8080/test */ int portNumber = 8080;    /* depends on deployed service port   */ String username = "test"; String password = "test123"; HttpClient client = null; try {     client = new HttpClient();     client.getState().setCredentials(     new AuthScope( url , portNumber ,AuthScope.ANY_REALM),     new UsernamePasswordCredentials( username , password ));     GetMethod method = new GetMethod(restUrl);     int returnCode = client.executeMethod(method);     if ( returnCode>= 200) {         Stri...