{"id":1749,"date":"2019-12-27T15:02:38","date_gmt":"2019-12-27T22:02:38","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1749"},"modified":"2019-12-27T15:02:38","modified_gmt":"2019-12-27T22:02:38","slug":"create-a-http-server-in-java","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2019\/12\/27\/create-a-http-server-in-java\/","title":{"rendered":"Create a HTTP Server in Java"},"content":{"rendered":"\n<p>Here is a quick tutorial on how to create a local embedded HTTP server in Java to serve JSON data.  The whole code in less than 30 lines on Java.<\/p>\n\n\n<pre><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.juixe.json.server;\n\nimport com.sun.net.httpserver.HttpExchange;\nimport com.sun.net.httpserver.HttpHandler;\nimport com.sun.net.httpserver.HttpServer;\n\nimport java.io.IOException;\nimport java.net.InetSocketAddress;\n\npublic class JSONServer implements HttpHandler {\n\n    public static void main(String&#x5B;] args) throws IOException  {\n        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);\n        server.createContext(&quot;\/data&quot;, new JSONServer());\n        server.setExecutor(null);\n        server.start();\n    }\n\n    @Override\n    public void handle(HttpExchange he) throws IOException {\n        String response = &quot;{\\&quot;message\\&quot;: \\&quot;Hello, HTTP Client\\&quot;}&quot;;\n\n        he.getResponseHeaders().set(&quot;Content-Type&quot;, &quot;application\/json;charset=UTF-8&quot;);\n        he.sendResponseHeaders(200, response.length());\n        he.getResponseBody().write(response.getBytes());\n        he.close();\n    }\n}\n<\/pre><\/pre>\n\n\n<p>The HttpServer and HttpHandler classes are part of the JRE, so no additional libraries are required.  Java also includes a HttpsServer which supports SSL.  In the HttpHandler, you can return any data but for this example we are returning a simple JSON response.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a quick tutorial on how to create a local embedded HTTP server in Java to serve JSON data. The whole code in less than 30 lines on Java. package com.juixe.json.server; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import java.io.IOException; import java.net.InetSocketAddress; public class JSONServer implements HttpHandler { public static void main(String&#x5B;] args) throws IOException [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,19,3],"tags":[828,802,55,415,99],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-sd","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1749"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=1749"}],"version-history":[{"count":4,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1749\/revisions"}],"predecessor-version":[{"id":1753,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1749\/revisions\/1753"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1749"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}