I just need help on the bolded part in Java, please.
Create a simple server that uses simple design patterns:
Singleton: Wrap your main server instance as a singleton class.
Factory: Your server should parse the routes and create a new Processor class depending on the route.
Builder: The process method of your Processor should use the Builder pattern to create a response.
Lazy Loading: Lazy load user's data on the first fetch.
The final response should be converted to JSON.
First, parse the URL, then use the factory to instantiate the correct Processor class.
Processor Class:
Has a process() method that returns a Response object.
Add a subclass per endpoint.
Takes a hashmap of key-value args.
Use a factory to create these, based on the endpoint.
Response Class:
This class gets stringified.
Needs a ResponseBuilder helper.
Needs Date.
Needs Params Map (String, String).
Needs response (Object, use Integer for ints).
Needs Response Code (String).
Server Class:
Wrap the simple server as a singleton and give it a method getInstance(), then run() to start.
Endpoints:
/math/add?a=&b=
Return a + b.
/math/add?a=
Return a + 1.
/posts?userid=
Single post.
/users?userid=
Single userId.
All others return the string "ERROR" as the response code.
Return Format:
You will need a response class that looks like this before passing to JSON.
"{
"date": "2019-02-10T20:40:59.257Z", // time that this was generated
"params": {
"a": "2"
},
"responseCode": "OK",
"response": 3
}