首页 > 解决方案 > 是否可以从 HTTP 填充 JHipster 上的实体?

问题描述

我刚开始使用 JHipster,并创建了一个包含我想要的实体的 .jh 文件。我想通过对已经有我需要的信息的现有 HTTP:PORT 发出 HTTP 请求来填充所有实体。

我想知道这是否可能?我目前正在尝试使用我在网上找到的这种方法:

private static void sendPOST() throws IOException {

    // URL obj = new URL(POST_URL);

    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("admin", "admin".toCharArray());
        }
    });

    String url = "http://localhost:8080/api/table-simple-tasks";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    // HttpURLConnection con = (HttpURLConnection) URL.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);

    // For POST only - START
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();
    // For POST only - END

    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("POST request not worked");
    }
}

标签: javajsonposthttprequestjhipster

解决方案


推荐阅读