You have 2 options to get the project.
-
Generate a new project from scratch:
-
- Create a new project at https://start.spring.io/ with these parameters
- a. Maven project
- b. Language: Java
- c. Spring boot: 2.6.4
- d. Group: com.rest
- e. Artifact: factoria
- g. Name: factoria
- h. Description: Demo REST project for Spring Boot
- i: Packaging: Jar
- j: Java: 11
- k: Dependencies: Spring Web
- Click on Generate and open it in IntelliJ
-
-
Clone the repo from https://github.com/Factoria-F5-Git/ApiDesign
- Add a AddController in the namespace com.rest.factoria.controllers
- Add the following imports
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;
- Add the following annotation to the AddController class
@RestController public class AddController {
- Create a GET method Hello
@GetMapping("hello") public String Hello(){ return "Hello"; }
- Open PostMan and run a Get Request
http://localhost:8765/hello
verifying that it returnsHello
- Add the following imports
- Extend the Hello endpoint in the AddController class to accept a parameter
name
. The call tohttp://localhost:8765/hello?name=James
will have to returnHello James
- Create a new GET endpoint named
total
in the AddController class.- It will have to return the value of a
totalValue
static int variable that you will have to define within the AddController class. Initialize it to 1. - Make sure the
http://localhost:8765/total
returns 1 via Postman
- It will have to return the value of a
- Create a new POST endpoint
add
using the@PostMapping("add")
annotation.- Make it accept a
valueToAdd
parameter of type int. - Add
valueToAdd
to total - Using Postman make a POST request to
http://localhost:8765/add
setting the Body parametervalueToAdd
to 2. - Make sure the
http://localhost:8765/total
returns 3 via Postman
- Make it accept a