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

Sending JSON's through HTTP Request, but the Response is NULL

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

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();
        }

    }
}

控制器中的 "receiver app is a simple spring-boot app waiting for "POST" 请求并将 JSON 的内容发送到数据库。

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

byte[] outputBytes=json.toString().getBytes("UTF-8")

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

它对我有用。 使用 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();
    }

  }
}