mardi 7 avril 2015

How to call a webservice in spring from the jersey client program

I have written the client program using the jersey framework but my webservice is of different project which uses spring framework. So I want to write the webservice using spring . When I wrote it something is going wrong .The webservice is not getting called. Here is my code ModelerServiceClient.java is written using jersey


ModelerServiceClient.java



package com.tcs.DataShare.Client;
import java.io.File;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
//import com.tcs.srl.smart.utility.PropertyReader;
import com.tcs.ngps.sip.modeler.utils.ProductConfiguration;
public class ModelerServiceClient {
static final Logger LOGGER = LoggerFactory
.getLogger(DataShareServiceClient.class);

private WebResource service;
private ClientResponse response;
private String serviceName;
private String vmAddress;
private String portNumber;
private String WAR_FILE_NAME_DATASHARE;
public ModelerServiceClient() {
try {
serviceName = "BPIngestorAppaaS";
vmAddress = ProductConfiguration
.getStringValueForProductProperty("vmAddress");
portNumber = ProductConfiguration
.getStringValueForProductProperty("portNumber");
System.out
.println("vm address:" + vmAddress + "port:" + portNumber);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WAR_FILE_NAME_DATASHARE = ProductConfiguration
.getStringValueForProductProperty("WAR_FILE_NAME_DATASHARE");

service = client.resource(UriBuilder.fromUri(
"http://" + vmAddress + ":" + portNumber + "/"
+ WAR_FILE_NAME_DATASHARE).build());
System.out.println("war name is: "+WAR_FILE_NAME_DATASHARE);
System.out.println("URL is: "+service);
} catch (NullPointerException nullex) {
LOGGER.error("Error occured - " + nullex.getMessage());
} catch (Exception ex) {
LOGGER.error("" + ex);
}
}
public ModelerServiceClient(String localhost, String port,
String serviceName) {
this.vmAddress = localhost;
this.portNumber = port;
this.serviceName = serviceName;
System.out.println("vm address:" + vmAddress + "port:" + portNumber);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WAR_FILE_NAME_DATASHARE = ProductConfiguration
.getStringValueForProductProperty("WAR_FILE_NAME_DATASHARE");

service = client.resource(UriBuilder.fromUri(
"http://" + vmAddress + ":" + portNumber + "/" + WAR_FILE_NAME_DATASHARE)
.build());
LOGGER.debug("WAR_FILE_NAME in the client program"+WAR_FILE_NAME_DATASHARE);
System.out.println("In the data share constructor"+WAR_FILE_NAME_DATASHARE);
System.out.println("service is" + service);
}
public HashMap createTable(String folderToBeZipped) {

LOGGER.debug("DataShareServiceClient :: zipFolder() : Calling zipFolderWithSubsequestFolder Service -> folderToBeZipped: "
+ folderToBeZipped);
HashMap map=new HashMap();
LOGGER.debug("before webservice ");
System.out.println("in the create table method in modelerServiceClient class");
response = service.path("dataModeler").path("ModelerService")
.path("createTablesFromEntity")
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, folderToBeZipped);
System.out.println("After calling response,in the create table method in modelerServiceClient class");
LOGGER.debug("INSIDE THE ZIP METHOD FOR CHECKING ZIP METHOD");
LOGGER.debug("after webservice ");
// InputStream inputStream = response.getEntityInputStream();


LOGGER.debug("DataShareServiceClient :: createTable() : Calling createTable() Service done");
return map;
}
}


DataShareCentralController.java is in spring


DataShareCentralController.java



package com.tcs.ngps.sip.restservices.controllers;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.tcs.ngps.sip.model.ModelerResponse;
import com.tcs.ngps.sip.modeler.Services.DataIngestionServices;
import com.tcs.ngps.sip.modeler.constants.IngesterConstants;
import com.tcs.ngps.sip.modeler.constants.ModelerConstants;
import com.tcs.ngps.sip.modeler.enumeration.TechnologyServices;
import com.tcs.ngps.sip.modeler.ingester.CreateTableUsingEntityJson;
import com.tcs.ngps.sip.modeler.tracker.StatusActivityTracker;
import com.tcs.ngps.sip.modeler.utils.Utilities;
import com.tcs.ngps.sip.restservices.viewmodels.AnalysisInputModel;
//import com.tcs.ngps.sip.Services.CIServices;
/*
import com.tcs.ngps.sip.Services.SearchServices;
import com.tcs.ngps.sip.commonUtitlities.CommonUtilities;
import com.tcs.ngps.sip.constants.RequestConstants;
import com.tcs.ngps.sip.model.RequestModel;
import com.tcs.ngps.sip.model.ResponceModel;
*/
import com.tcs.ngps.sip.restservices.viewmodels.ResponseModel;
//@Path("/ModelerService")
@RestController
@EnableWebMvc
public class DataShareCentralController {
static Logger logger = Logger.getLogger(ModelerCentralController.class);

DataShareCentralController()
{
System.out.println("In the DataShareCentralController ");
}
// @Path("/createTablesFromEntity")
//@POST
//@Produces("application/json")
@ResponseBody
@RequestMapping(value="/ModelerService/createTablesFromEntity", method= RequestMethod.GET,produces = APPLICATION_JSON_VALUE)
public void CreateTableFromEntity(@Context HttpServletRequest requestObj,String serviceData )
{
System.out.println("done dana done");
JSONParser parser = new JSONParser();
JSONObject object = new JSONObject();
try {
System.out.println("in the controller");
JSONObject serviceJSON = (JSONObject) parser.parse(serviceData);
String datashareURL = (String) serviceJSON
.get(ModelerConstants.DATASHARE_URL);

CreateTableUsingEntityJson createTableUsingEntityJson=new CreateTableUsingEntityJson();
createTableUsingEntityJson.getData();

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


Please help me in resolving the issue....


Aucun commentaire:

Enregistrer un commentaire