我写了一个球衣客户端代码来调用webservice.And工作正常。现在,我需要使用Spring休息模板来调用webservice。所以请帮我把球衣代码转换成春季4.0。 这是我的球衣代码。 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 the 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 THE ZIP METHOD FOR CHECKING ZIP METHOD");
InputStream inputStream = response.getEntityInputStream();
LOGGER.debug("DataShareServiceClient :: zipFolder() : Calling zipFolderWithSubsequestFolder Service done");
return inputStream;
}
}
答案 0 :(得分:1)
您可以通过简单的搜索找到它。
根据春季的this教程,您可以这样做:
// Set the Content-Type header
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application","json"));
HttpEntity<String> requestEntity = new HttpEntity<String>(folderToBeZipped, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson and String message converters
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP POST request, marshaling the request to JSON, and the response to a String
ResponseEntity<InputStream> responseEntity = restTemplate.exchange(your_url, HttpMethod.POST, requestEntity, InputStream.class);
String result = responseEntity.getBody();
希望这会有所帮助。