Question: Write an HTTP service (using Spring) that acts as a calculator with a simple history functionality. You should create one or more new Java classes
Write an HTTP service (using Spring) that acts as a calculator with a simple history functionality.
You should create one or more new Java classes that have the @RestController annotation present. Use the @GetMapping, @PostMapping, etc annotations as necessary to set up HTTP request handlers. For this assignment, please create request handlers that respond to the following mathematical requests:
- A GET request to /add/{num1}/{num2} - Responds to the request with the sum of num1 and num2
- A GET request to /sub/{num1}/{num2} - Responds with the result of subtracting num2 from num1
- A GET request to /mult/{num1}/{num2} - Responds with the product of num1 and num2
- A GET request to /div/{num1}/{num2} - Responds with the result of dividing num1 by num2
In addition to performing each operation, in your request handler method, please also store a record of the operation somehow. You should store the following data points as separate values:
- The value of num1
- The value of num2
- The operation
- The time the operation was performed
Please create a Java class that will allow you to capture all of the information about a single operation in a single Java Object. Finally, please set up your server to handle the following history operations:
- A GET request to /history - Responds with all of the information above about all operations that have been run since the calendar app was started (or the DELETE history operation runs). The output should look like an array of JSON objects.
- A DELETE request to /history - Clears the history so that future GET requests to /history only show new operations that have been run since the DELETE. This request should return the number of items deleted
Sample code-------------------------
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController public class TestController { private final List people = new ArrayList<>(); @GetMapping("/people/{id}") public Person sayHello( @PathVariable int id ) { for(Person person: people) { if(person.id == id) { return person; } } return null; } @GetMapping("/people") public List allNames() { return people; } @PostMapping("/people") public String createPerson(@RequestBody Person person) { people.add(person); return "Success"; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
