背景:
webアプリからprivateブロックチェーンをアクセスするために、javaでjar/warを作ろうと思ってます。
webアプリ <---> javaアプリ <---> privateブロックチェーン
javaアプリに実現したいこと:
HttpURLConnectionより、ブロックチェーンをアクセスして、Responseを返す
ResponseはJsonデータなので、Jsonデータ処理(整形、Mapに転換など)が必要です。
実際のコードなら、↓をご参照ください。
※作成中ですが、json処理を入れていない。json処理を入れると他のjarをIMPORTする必要がある
java
1import java.io.BufferedReader; 2import java.io.DataOutputStream; 3import java.io.IOException; 4import java.io.InputStreamReader; 5import java.io.StringWriter; 6import java.net.HttpURLConnection; 7import java.net.MalformedURLException; 8import java.net.URL; 9 10public class Main { 11 12 private static String urlBase = "http://127.0.0.1:8080"; 13 private static String urlOperate = "http://127.0.0.1:8081"; 14 15 public static void main(String[] args) { 16 17 try { 18 if (args == null || args.length == 0) { 19 getLatest(); 20 } else { 21 switch (args[0]) { 22 case "latest": 23 getLatest(); 24 break; 25 case "address": 26 getNewAddress(); 27 break; 28 case "txes": 29 getTxes(args[1]); 30 break; 31 case "getTokensByAddress": 32 getAllTokensByAddress(args[1]); 33 break; 34 default: 35 System.out.println("入力ミスです。"); 36 } 37 } 38 39 } catch (MalformedURLException e) { 40 e.printStackTrace(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 } 45 46 public static void getLatest() { 47 try { 48 URL url = new URL(urlOperate + "/blockchain/latest"); 49 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 50 System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage()); 51 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 52 String input; 53 54 while ((input = br.readLine()) != null) { 55 System.out.println(input); 56 } 57 br.close(); 58 59 } catch (MalformedURLException e) { 60 e.printStackTrace(); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 } 65 66 public static void getNewAddress() { 67 try { 68 URL url = new URL(urlOperate + "/addresses"); 69 70 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 71 conn.setRequestMethod("POST"); 72 conn.connect(); 73 74 System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage()); 75 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 76 String input; 77 78 while ((input = br.readLine()) != null) { 79 System.out.println(input); 80 } 81 br.close(); 82 } catch (MalformedURLException e) { 83 e.printStackTrace(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 } 88 89 public static String getTxes(String txId) { 90 try { 91 URL url = new URL(urlOperate + "/txes/" + txId); 92 93 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 94 95 System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage()); 96 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 97 String input; 98 99 while ((input = br.readLine()) != null) { 100 System.out.println(input); 101 } 102 103 br.close(); 104 return ""; 105 106 } catch (MalformedURLException e) { 107 e.printStackTrace(); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } 111 return ""; 112 } 113 114 public static String getAllTokensByAddress(String address) { 115 try { 116 URL url = new URL(urlOperate + "/addresses/" + address + "/tokens"); 117 118 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 119 120 System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage()); 121 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 122 String input; 123 124 while ((input = br.readLine()) != null) { 125 System.out.println(input); 126 } 127 128 br.close(); 129 return ""; 130 131 } catch (MalformedURLException e) { 132 e.printStackTrace(); 133 } catch (IOException e) { 134 e.printStackTrace(); 135 } 136 return ""; 137 } 138}
今の考え:
javaでjarファイルを作成することにしました。
warなら、Tomcat等にデプロイしなけければなりません。
jarなら、そのまま置いといて、簡単にwebアプリに呼ばれるはずです。
できるだけ、簡単なものに作りたいので、jarで作成しろうと思います。
Jsonデータ処理に関しては、他のjarをimportする必要があるので、作成する際に、Jar Jar Linksもしないと行けません。
実現には可能でしょうか?
ほかもっと簡単な方法があれば、ぜひご教授ください。
開発環境:
windows
eclipse
サーバ環境:
ubuntu
回答2件
あなたの回答
tips
プレビュー