面向对象Java设计如何重用http客户端 公共类客户端{ 公共静态列表编码文本(字符串文本)引发IOException{ 字符串url=”http://127.0.0.1:5000/encode"; 列表向量=sendPOST(url,文本); 返回向量; } 私有静态列表sendPOST(字符串url、字符串文本)引发IOException{ CloseableHttpClient httpClient=HttpClients.createDefault(); HttpPost HttpPost=新的HttpPost(url); String requestBody=String.format(“{\“text\”:\%s\“}”,text); StringEntity requestEntity=新的StringEntity(requestBody); //发送JSON数据 httpPost.setEntity(requestEntity); setHeader(“接受”、“应用程序/json”); setHeader(“内容类型”、“应用程序/json”); CloseableHttpResponse response=httpClient.execute(httpPost); InputStream source=response.getEntity().getContent();//获取实体中的数据 Reader Reader=新的InputStreamReader(源); Gson Gson=新的Gson(); JsonResponse json=gson.fromJson(reader,JsonResponse.class); httpClient.close(); 返回json.bert_向量; } 私有类JsonResponse{ //此类用于Gson将JSON响应解析为对象 //这个变量需要匹配JSON响应中的键,所以它不是驼峰式的 私有列表bert_向量; } }

面向对象Java设计如何重用http客户端 公共类客户端{ 公共静态列表编码文本(字符串文本)引发IOException{ 字符串url=”http://127.0.0.1:5000/encode"; 列表向量=sendPOST(url,文本); 返回向量; } 私有静态列表sendPOST(字符串url、字符串文本)引发IOException{ CloseableHttpClient httpClient=HttpClients.createDefault(); HttpPost HttpPost=新的HttpPost(url); String requestBody=String.format(“{\“text\”:\%s\“}”,text); StringEntity requestEntity=新的StringEntity(requestBody); //发送JSON数据 httpPost.setEntity(requestEntity); setHeader(“接受”、“应用程序/json”); setHeader(“内容类型”、“应用程序/json”); CloseableHttpResponse response=httpClient.execute(httpPost); InputStream source=response.getEntity().getContent();//获取实体中的数据 Reader Reader=新的InputStreamReader(源); Gson Gson=新的Gson(); JsonResponse json=gson.fromJson(reader,JsonResponse.class); httpClient.close(); 返回json.bert_向量; } 私有类JsonResponse{ //此类用于Gson将JSON响应解析为对象 //这个变量需要匹配JSON响应中的键,所以它不是驼峰式的 私有列表bert_向量; } },java,http,oop,Java,Http,Oop,这更像是一个设计问题 我对Java比较陌生,我编写了这个包以便导入它并在其他地方使用。我只需要使用BertClient.encodeText()发送POST请求并在其他Java文件中获取响应,因此我认为没有必要实例化这个类。我可以直接调用这个方法。然后我意识到,使用这段代码,每次发送POST请求时,我都在构建一个新的httpClient,我想这显然是错误的 url现在是硬编码的,用于测试,但稍后将由此类中的方法检索,读取我存储在AWS secrets manager中的url 我想知道我应该如何

这更像是一个设计问题

我对Java比较陌生,我编写了这个包以便导入它并在其他地方使用。我只需要使用
BertClient.encodeText()
发送POST请求并在其他Java文件中获取响应,因此我认为没有必要实例化这个类。我可以直接调用这个方法。然后我意识到,使用这段代码,每次发送POST请求时,我都在构建一个新的httpClient,我想这显然是错误的

url现在是硬编码的,用于测试,但稍后将由此类中的方法检索,读取我存储在AWS secrets manager中的url

我想知道我应该如何设计这个类,以便它重用代码,而不创建不必要的新对象

请使用修改后的示例代码对我的原始代码进行解释

此外,我不确定我的问题标题是否正确,很难给它一个正确的名称,所以如果你对此有任何建议,我将做出更改

public class BertClient {

    public static List<Float> encodeText(String text) throws IOException {
        String url = "http://127.0.0.1:5000/encode";
        List<Float> bertVector = sendPOST(url, text);
        return bertVector;
    }

    private static List<Float> sendPOST(String url, String text) throws IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        String requestBody = String.format("{\"text\":\"%s\"}", text);
        StringEntity requestEntity = new StringEntity(requestBody);
        // send a JSON data
        httpPost.setEntity(requestEntity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = httpClient.execute(httpPost);
        InputStream source = response.getEntity().getContent(); //Get the data in the entity
        Reader reader = new InputStreamReader(source);
        Gson gson = new Gson();
        JsonResponse json = gson.fromJson(reader, JsonResponse.class);
        httpClient.close();

        return json.bert_vector;
    }

    private class JsonResponse {
        //This class is for Gson to parse JSON response into Object
        //This variable needs to match the key in JSON response, so it's not camel case
        private List<Float> bert_vector;
    }
}