To call a third-party API in Java with an object and a list collection as parameters, you can use the following code:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        String apiUrl = "http://10.10.1.211:85/api/PushSquareI/HosPushPrescription";
        String token = "<your token>";

        // Create the object and list collection
        YourObject obj = new YourObject();
        // Set the properties of the object

        List<YourItem> items = new ArrayList<>();
        // Add items to the list

        // Create the JSON payload
        Gson gson = new Gson();
        String jsonPayload = gson.toJson(obj);

        // Create the HTTP client and request
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(apiUrl);
            
            // Set the Authorization header
            httpPost.setHeader("Authorization", token);
            
            // Set the JSON payload
            StringEntity requestEntity = new StringEntity(jsonPayload);
            httpPost.setEntity(requestEntity);

            // Send the request and get the response
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();

            // Process the response
            if (responseEntity != null) {
                String responseString = EntityUtils.toString(responseEntity);
                System.out.println(responseString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Make sure to replace <your token> with your actual token, and YourObject and YourItem with the appropriate class names for your object and list items

http1010121185apiPushSquareIHosPushPrescriptionjsondhyPOSTAuthorization your tokenjava调第三方接口参数是一个对象和一个list集合

原文地址: https://www.cveoy.top/t/topic/ivZo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录