如何将与Curl命令相关的REST调用转换为java客户端?

时间:2015-12-28 10:24:45

标签: java rest curl httpclient multipartform-data

curl -v 
 -F "sharedsecret=YourIntegrationPassword" 
 -F "content=@BatchDataFile.zip;type=application/octet-stream" 
 -F "event=Import Batch Data" 
 -F clienttype="Curl" -F clientinfo="OS=Linux:Host=myhost" -F clientversion="1.0" 
 https://www.sample.com/sell/fileupload?realm=YourSiteID

我在linux终端上使用curl命令成功调用了上面的REST调用。现在,我想使用Java RESTClient调用服务(REST服务器)。怎么做?

4 个答案:

答案 0 :(得分:1)

有一个适用于java的轻量级卷曲包装API。 看看:CurlJava

答案 1 :(得分:0)

如果您可以拥有Spring依赖关系,那么请查看Consuming RESTful webservice

答案 2 :(得分:0)

你可以编写自己的java Rest客户端, 从那段代码中你可以打电话给你休息。

公共类ClientJerseyGet {

public static void main(String[] args) {

    try {

        Client client = Client.create();
        String restUrl="https://www.sample.com/sell/fileupload?realm=YourSiteID";
        WebResource webResource = client.resource(restUrl);

        ClientResponse response = webResource.accept("application/xml")
                .get(ClientResponse.class);
        String output = response.getEntity(String.class);

        System.out.println("Server response : " + response.getStatus());
        System.out.println();
        System.out.println(output);

        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

了解更多详情,您可以关注:http://entityclass.in/rest/jerseyClientGetXml.htm

答案 3 :(得分:0)

import javax.ws.rs.core.MediaType;

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.multipart.FormDataMultiPart;

public class AribaClient {

    public static void main(String argsp[]) {

        AribaClient clientRequest = new AribaClient();

        clientRequest.getResponse();
    }

    private void getResponse() {
        try {

            Client client = Client.create();
            WebResource webResource = client
                    .resource("https://www.sample.com/sell/fileupload?realm=YourSiteID");

            FormDataMultiPart formData = new FormDataMultiPart();
            formData.field("name", "value");
            formData.field("file", "D:/repository/moved Files/Purchase.zip");
            formData.field("name1", "value1");

            ClientResponse response = webResource.type(
                    MediaType.MULTIPART_FORM_DATA_TYPE).post(
                    ClientResponse.class, formData);

            // ClientResponse response =
            // webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,
            // formData);
            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response);
            } else {
                System.out.println("Response :: " + response.getStatus());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

这是我能够将表单值发布到REST服务器的方式。