Last active
March 25, 2026 15:45
-
-
Save RainerRoss/566d16819034dd9b783dfa6c96d2125c to your computer and use it in GitHub Desktop.
Java Webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*-------------------------------------------------------------------*/ | |
| /* */ | |
| /* Start Java Program */ | |
| /* */ | |
| /*----------------* */ | |
| /* R.ROSS 03.2026 * */ | |
| /*-------------------------------------------------------------------*/ | |
| PGM | |
| DCL VAR(&HOME) TYPE(*CHAR) LEN(64) | |
| DCL VAR(&FILE1) TYPE(*CHAR) LEN(64) | |
| DCL VAR(&FILES) TYPE(*CHAR) LEN(512) | |
| MONMSG MSGID(CPF0000) | |
| /*-------------------------------------------------------------------*/ | |
| /* Processing */ | |
| /*-------------------------------------------------------------------*/ | |
| CHGVAR VAR(&HOME) + | |
| VALUE('/Home/Java/bin') | |
| CHGVAR VAR(&FILE1) + | |
| VALUE('/Home/Java/jar/json-20250517.jar') | |
| CHGVAR VAR(&FILES) + | |
| VALUE(&HOME *tcat ':' *tcat &FILE1) | |
| JAVA CLASS(JavaWebservice) CLASSPATH(&FILES) | |
| ENDPGM | |
| /*-------------------------------------------------------------------*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //------------------------------------------------------------------------------// | |
| // // | |
| // Java Webservice // | |
| // // | |
| //----------------- // | |
| // R.Ross 03.2026 * // | |
| //------------------------------------------------------------------------------// | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStream; | |
| import java.net.InetSocketAddress; | |
| import java.nio.charset.StandardCharsets; | |
| import java.util.concurrent.Executors; | |
| import org.json.JSONObject; | |
| import com.sun.net.httpserver.HttpExchange; | |
| import com.sun.net.httpserver.HttpHandler; | |
| import com.sun.net.httpserver.HttpServer; | |
| public class JavaWebservice { | |
| private static final int port = 8060; | |
| private static String response; | |
| private static boolean success = true; | |
| private static String message = ""; | |
| private static JSONObject json; | |
| private record Kunde(String name, int menge, double umsatz, boolean aktiv) {} | |
| public static void ParseJson(String data) { | |
| json = new JSONObject(data); | |
| Kunde kunde = new Kunde(json.getString("name"), json.getInt("menge"), json.getDouble("umsatz"), json.getBoolean("aktiv")); | |
| String name = kunde.name; | |
| int menge = kunde.menge; | |
| double umsatz = kunde.umsatz; | |
| boolean aktiv = kunde.aktiv; | |
| System.out.println("Kunde.: " + kunde); | |
| System.out.println("Name..: " + name); | |
| System.out.println("Menge.: " + menge); | |
| System.out.println("Umsatz: " + umsatz); | |
| System.out.println("Aktiv.: " + aktiv); | |
| } | |
| static class MyHandler implements HttpHandler { | |
| @Override | |
| public void handle(HttpExchange httpExchange) throws IOException { | |
| InputStream stream = httpExchange.getRequestBody(); | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); | |
| StringBuilder content = new StringBuilder(); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| content.append(line); | |
| } | |
| ParseJson(content.toString()); | |
| JSONObject obj = new JSONObject(); | |
| obj.put("success", success); | |
| obj.put("message", message); | |
| response = obj.toString(); | |
| httpExchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8"); | |
| httpExchange.sendResponseHeaders(200, response.length()); | |
| OutputStream os = httpExchange.getResponseBody(); | |
| os.write(response.getBytes()); | |
| os.close(); | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); | |
| server.createContext("/", new MyHandler()); | |
| server.setExecutor(Executors.newCachedThreadPool()); | |
| server.start(); | |
| System.out.println("Server started on port " + port); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| values QSYS2.HTTP_POST( | |
| 'http://172.16.0.120:8060/JavaWebservice', | |
| json_object( | |
| 'name' : 'Bandweberei Müller', | |
| 'menge' : 6350, | |
| 'umsatz' : 25386.25, | |
| 'aktiv' : true | |
| ), | |
| json_object( | |
| 'header' : 'content-type,application/json' | |
| ) | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ctl-opt main(main) dftactgrp(*no); | |
| //------------------------------------------------------------------// | |
| // // | |
| // Test - Java Webservice // | |
| // // | |
| //----------------- // | |
| // R.Ross 03.2026 * // | |
| //------------------------------------------------------------------// | |
| // Main // | |
| //------------------------------------------------------------------// | |
| dcl-proc main; | |
| dcl-ds DsKunde qualified inz; | |
| Name varchar(40); | |
| Menge int(10); | |
| Umsatz packed(11:2); | |
| Aktiv ind; | |
| end-ds; | |
| dcl-s LocJson varchar(1024) ccsid(*utf8); | |
| dcl-s LocResult varchar(1024); | |
| DsKunde.Name = 'Bandweberei Müller'; | |
| DsKunde.Menge = 6350; | |
| DsKunde.Umsatz = 25386.25; | |
| DsKunde.Aktiv = *on; | |
| exec sql set :LocJson = | |
| json_object( | |
| 'name' : trim(:DsKunde.Name), | |
| 'menge' : :DsKunde.Menge, | |
| 'umsatz' : :DsKunde.Umsatz, | |
| 'aktiv' : :DsKunde.Aktiv | |
| ); | |
| exec sql | |
| set :LocResult = QSYS2.HTTP_POST( | |
| 'http://172.16.0.120:8060/JavaWebservice', | |
| :LocJson, | |
| json_object('header':'Content-Type,application/json') | |
| ); | |
| end-proc; | |
| //------------------------------------------------------------------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment