Ravi Gaddam

Ravi Gaddam

  • NA
  • 42
  • 13.7k

Need java Client Example for Spring Rest Service

Sep 17 2018 4:00 AM
Hi Folks ,
 
Could you please help me to write Java client for Spring Rest Service but am seeing issues like Http Status 400(Bad request) , 500(Internal Server) Errors. I will give my sample template code below .Please advise any changes are required in service and client applications.
 
Service :
------------ 
 
@RequestMapping(value = "/employeeAccessRequest", method = RequestMethod.GET)
public @ResponseBody EmployeeAccessResponse getEmployeeAccess(@RequestBody EmployeeAccessRequest employeeAccessRequest) {
EmployeeAccessResponse  employeeAccessResponse =null; 
--------------------------------------
------------------------------------
return employeeAccessResponse ; 
 
 
 Client:
--------- 
 
/* GET */
private static EmployeeAccessResponse getEmployeeAccess(String xml){
System.out.println("Testing getEmployeeSecureAccess API----------");
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
RestTemplate restTemplate = new RestTemplate(requestFactory);
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<String> request = new HttpEntity<String>(xml, headers);
final EmployeeAccessResponse employeeAccessResponse = restTemplate.postForObject(REST_SERVICE_URI+"/employeeAccessRequest",request, EmployeeAccessResponse.class);
System.out.println(employeeAccessResponse);
return employeeAccessResponse;
}
 
 
Thnaks
Ravi Gaddam