是否可以通过HTTP在JHipster上填充实体?

时间:2018-10-16 08:58:29

标签: java json post httprequest 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");
    }
}

0 个答案:

没有答案