Tuesday 8 December 2015

How to access Restful web service using Java Http Client


Step-1

First we need to add these dependencies in pom.xml
<!-- GSON -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

<!-- http client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>


class ProgramService {

     @Override
public void addProgram(ProgramVO programVo) {

DefaultHttpClient client = new DefaultHttpClient();

//This code converting Java Object into JSON String
String json = new GsonBuilder().create().toJson(programVo, ProgramVO.class);

HttpResponse response =null;
try{

      //Define a postRequest request
        HttpPost postRequest = new HttpPost("http://www.gpsprogramys.co.in/com.gps.quiz/android/v1/program/padd");
         
        //Set the API media type in http content-type header
         //Here we are setting content-type & Accept as JSON
        postRequest.setHeader("content-type", "application/json");
        postRequest.setHeader("Accept", "application/json"); 
       
        //Set the request post body
        postRequest.setEntity(new StringEntity(json));
         
        //Send the request; It will immediately return the response in HttpResponse object if any
        response = client.execute(postRequest);
        System.out.println(response.toString());
       
        //verify the valid error code first
        int statusCode = response.getStatusLine().getStatusCode();
       
        System.out.println(statusCode);
        System.out.println(statusCode);
         /*if (statusCode != 201)
        {
            throw new RuntimeException("Failed with HTTP error code : " + statusCode);
        }*/
}
catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
finally
    {
        //Important: Close the connect
        client.getConnectionManager().shutdown();
    }
}
}

Rest Provider code :-

@Controller
@Scope("request")
@RequestMapping(value = "/v1/program")
public class ProgramController {

@Autowired
@Qualifier("ProgramService")
private IProgramService iProgramService;

public ProgramController() {
}

@RequestMapping(value =ProgramURIConstant.GET_PROGRAM, method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
ProgramVO findProgramById(@PathVariable("pid") String programid) {
ProgramVO result = iProgramService.findProgramByQId(programid);
return result;
}

@RequestMapping(value = ProgramURIConstant.ADD_PROGRAM, method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String addProgram(@RequestBody ProgramVO programVO) {
programVO.setTimestamp(DateUtils.getCurrentTimeIntoTimestamp());
String result = iProgramService.addProgram(programVO);
return result;
}

@RequestMapping(value = "padd", method = RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public @ResponseBody
String addProgramText(@RequestBody ProgramVO programVO) {
programVO.setTimestamp(DateUtils.getCurrentTimeIntoTimestamp());
String result = iProgramService.addProgram(programVO);
return result;

}
}

No comments:

Post a Comment