Read Me
--- How to run ---
Simply run the following command:
java -jar jars/json-httpd.jar
And the server will start at port 8080. The server will use the folder
"webroot" in the servers working directory. Both port and web root can
be configured via options, see "java -jar jars/json-httpd.jar --help".
--- How to use as a library ---
Example use: (Check docs for more info)
try {
httpd = new HTTPServer(8080, 20); // Start server at port 8080.
httpd.addService(new ExampleService()); // Add an service.
httpd.start(); // Start the server
} catch (IOException e) {
System.out.println("Failed to start the server");
}
--- How to write a service ---
1. Create a class.
2. Create a method that takes arguments of type String, int, long, float
or doble, return value should be a JSON formated string.
3. Add the @JsonServiceMethod annotation to the method.
4. Write the code and generate a JSON formated return value
with the JSONWriter class. (For more info about JSONWriter check
http://www.stringtree.org/stringtree-json.html).
--- How to write a custom handler ---
A custom handler is a method that takes a request and send a response to that
request.
1. Create a class with implements CustomHandler
2. Implement the method "getPath", this is the path for which the handler will
handle request on. For exampel "/test/".
3. Implement the method handle, it takes a HTTPRequest and a OutputStream.
The HTTPRequest contains the request parameters (for example path).
The OutputStream is used to send data back to the client.
To create a response header you can use the method HTTPResponseBuilder.buildResponse
which takes the HTTPRequest object and a HTTPPayload object. In the HTTPPayload object
you can set the content type and the HTTP Status for example. Look in TestHandler.java
for an example.