- Use Spring Boot Web to build a simple API
- Use RestController classes to map HTTP requests to class methods
- Fork this repository
- Clone your fork to your machine
- Open the project in IntelliJ
- In the
CounterController
class, implement the requirements below
You're going to build a simple counter API that stores a number in memory, then provide routes to increment, decrement and retrieve the value of the number.
Use this documentation as a reference guide: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/package-summary.html
- Annotate the
CounterController
class appropriately to set it up as a REST controller, and map all requests ofhttp://localhost:4000/counter
to it - Create a GET method that responds with the current value of the counter. Its initial value should always be 0
- Create a GET method that maps to the route
http://localhost:4000/counter/increment
that increases the counter by 1 and responds with the updated value. The method you created in step 2 should also now respond with the updated value - Create a GET method that maps to the route
http://localhost:4000/counter/decrement
that decreases the counter by 1 and responds with the updated value
- Create a GET method that maps to the route
http://localhost:4000/counter/custom/{name}
{name}
is a path variable, the client can provide any string they want- If a counter with the provided name does not already exist, a new counter should be created with the name provided in the path variable, initialised to a value of 0
- If a counter with the provided name does exist, the route should respond with the current value of the named counter
- Create a GET method that maps to the route
http://localhost:4000/counter/custom/{name}/increment
- If a counter with the provided name exists, increase the value of that counter by 1 and then respond with the new value
- If a counter with the provided name does not exist, it should be created and initialised with a value of 1
- The route you created in step 1 should respond appropriately
- Create a GET method that maps to the route
http://localhost:4000/counter/custom/{name}/decrement
- If a counter with the provided name exists, decrease the value of that counter by 1 and then respond with the new value
- If a counter with the provided name does not exist, it should be created and initialised with a value of -1
- The previous routes you created should respond appropriately
Explore the documentation of the Spring Boot Web framework as much as you are able, you'll be using it for almost everything going forward. We'll be introducing new components of the framework in future exercises.
Try building your own simple API using what you've learned so far.