How To Use Spring RESTTemplate To Post Data to a Web Service -
i have written jersey client code call webservice.and working fine. insteade of jersey have use spring rest template call webservice . please me in converting jersey code spring 4.0. here jersey code. serviceclient.java
package com.api.client; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.inputstream; 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.datashare.dao.configurelogdao; import com.tcs.ngps.sip.modeler.utils.productconfiguration; public class serviceclient { static final logger logger = loggerfactory .getlogger(serviceclient.class); private webresource service; private clientresponse response; private string servicename; private string vmaddress; private string portnumber; private string war_file_name; public serviceclient(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 = productconfiguration .getstringvalueforproductproperty("datashare_server_war_file_name"); service = client.resource(uribuilder.fromuri( "http://" + vmaddress + ":" + portnumber + "/" + war_file_name) .build()); logger.debug("war_file_name in client program"+war_file_name); system.out.println("service is" + service); } public string getservicename() { return servicename; } public void setservicename(string servicename) { this.servicename = servicename; } public string getvmaddress() { return vmaddress; } public void setvmaddress(string vmaddress) { this.vmaddress = vmaddress; } public string getportnumber() { return portnumber; } public void setportnumber(string portnumber) { this.portnumber = portnumber; } public inputstream zipfolder(string foldertobezipped,string transactionid) { logger.debug("serviceclient :: zipfolder() : calling zipfolder service -> foldertobezipped: " + foldertobezipped); string header = getservicename(); response = service.path("rest").path("datashareservice") .path("zipfolder") .type(mediatype.application_json).header("header", header) .post(clientresponse.class, foldertobezipped); logger.debug("inside zip method checking zip method"); inputstream inputstream = response.getentityinputstream(); logger.debug("datashareserviceclient :: zipfolder() : calling zipfolderwithsubsequestfolder service done"); return inputstream; } }
you can find out simple search.
according this tutorial spring can this:
// set content-type header httpheaders requestheaders = new httpheaders(); requestheaders.setcontenttype(new mediatype("application","json")); httpentity<string> requestentity = new httpentity<string>(foldertobezipped, requestheaders); // create new resttemplate instance resttemplate resttemplate = new resttemplate(); // add jackson , string message converters resttemplate.getmessageconverters().add(new mappingjackson2httpmessageconverter()); resttemplate.getmessageconverters().add(new stringhttpmessageconverter()); // make http post request, marshaling request json, , response string responseentity<inputstream> responseentity = resttemplate.exchange(your_url, httpmethod.post, requestentity, inputstream.class); string result = responseentity.getbody();
hope helps.
Comments
Post a Comment