通过HTTP请求发送JSON,但响应为NULL

问题描述 投票:0回答:2

我需要向我的另一个应用程序发送一些JSON,以便将它们存储在数据库中。我的问题是另一个应用程序不断获取NULL参数。这是我的“发件人”应用程序:

public class Main {

    public static void main(String[] args) {
        ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();

        for (int i = 0; i <= 10; i++) {
            byte[] array = new byte[7]; // length is bounded by 7
            new Random().nextBytes(array);
            String generatedString = new String(array, Charset.forName("UTF-8"));
            long id = new Random().nextLong() & 0xffffffffL;
            employeeQueue.add(new Employee(id, generatedString));
        }


        try {
            while (!employeeQueue.isEmpty()) {
                JSONObject json = new JSONObject(employeeQueue.remove());

                URL url = new URL("http://localhost:8080/postgressApp/createEmp");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-Type", "application/json; utf-8");
                con.setRequestProperty("Accept", "application/json");
                con.setDoOutput(true);

                OutputStream os = con.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
                try {
                    json.write(osw);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                osw.flush();
                osw.close();    
                os.close();

                try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
                    StringBuilder response = new StringBuilder();
                    String responseLine = null;
                    while ((responseLine = br.readLine()) != null) {
                        response.append(responseLine.trim());
                    }
                    System.out.println("Response from server: " + response.toString());
                }

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

“接收器应用程序是一个简单的spring-boot应用程序,它等待Controller中的“ POST”请求并将JSON的内容发送到数据库。

java json post httprequest outputstream
2个回答
1
投票

将JsonObject转换为字节数组然后写入

字节[] outputBytes = json.toString()。getBytes(“ UTF-8”)

OutputStream os = con.getOutputStream();os.write(outputBytes);


0
投票

它正在为我工​​作。使用Gson将Java Dto解析为JSON类型。下载com.google.code.gson.jar-2.2.4版本。在您的代码中,我遇到了解析问题。

public class Main {

public static void main(String[] args) {
    ConcurrentLinkedQueue<Employee> employeeQueue = new ConcurrentLinkedQueue<>();

    for (int i = 0; i <= 10; i++) {
        byte[] array = new byte[7]; // length is bounded by 7
        new Random().nextBytes(array);
        String generatedString = new String(array, Charset.forName("UTF-8"));
        long id = new Random().nextLong() & 0xffffffffL;
        employeeQueue.add(new Employee(id, generatedString));
    }


    try {
        while (!employeeQueue.isEmpty()) {

          Gson gson = new GsonBuilder().setPrettyPrinting().create();  


                String jsonEmp = gson.toJson(employeeQueue.remove());

          //  JSONObject json = new JSONObject(employeeQueue.remove());

            URL url = new URL("http://localhost:8080/postgressApp/createEmp");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; utf-8");
            con.setRequestProperty("Accept", "application/json");
            con.setDoOutput(true);

            OutputStream os = con.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
            try {
                 osw.write(jsonEmp);
               // json.write(osw);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            osw.flush();
            osw.close();    
            os.close();

            try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine = null;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println("Response from server: " + response.toString());
            }

        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.