|
| 1 | +# Spring Boot REST API Exercise |
| 2 | + |
| 3 | +## Goal |
| 4 | + |
| 5 | +The goal of this exercise is to learn how to use GitHub Copilot, using an exercise that consist of building a REST API using Spring Boot. |
| 6 | + |
| 7 | +## Exercises |
| 8 | + |
| 9 | +We have created a Spring Boot project with some files already created, you can find the project in the folder **exercisefiles/springboot**. |
| 10 | + |
| 11 | +Let's start copiloting!!! |
| 12 | + |
| 13 | +### 1. Create the code to handle a simple GET request |
| 14 | + |
| 15 | +Move to the 'DemoController.java' file and start writing the code to handle a simple GET request. In this first exercise, we have provided a comment that describes the code you need to generate. Just press enter and wait a couple of seconds, Copilot will generate the code for you. |
| 16 | + |
| 17 | +There is already a unit test implemented for this exercise, you can run it using the command `mvn test` before and after to validate that the code generated by Copilot is correct. |
| 18 | + |
| 19 | +Then, create a new unit test for the case when no key is provided in the request. |
| 20 | + |
| 21 | +After every exercise, feel free to package and run your application to test it. |
| 22 | + |
| 23 | +Package: `mvn package` |
| 24 | + |
| 25 | +Run: `mvn spring-boot:run` |
| 26 | + |
| 27 | +Test: `curl -v http://localhost:8080/hello?key=world` |
| 28 | + |
| 29 | +### 2. Dates comparison |
| 30 | + |
| 31 | +New operation under /diffdates that calculates the difference between two dates. The operation should receive two dates as parameter in format dd-MM-yyyy and return the difference in days. |
| 32 | + |
| 33 | +Additionally, create a unit test that validates the operation. |
| 34 | + |
| 35 | +From now on, you will have to create the unit tests for every new operation. Wasn't it easy with Copilot? |
| 36 | + |
| 37 | +### 3. Validate the format of a spanish phone |
| 38 | + |
| 39 | +Validate the format of a spanish phone number (+34 prefix, then 9 digits, starting with 6, 7 or 9). The operation should receive a phone number as parameter and return true if the format is correct, false otherwise. |
| 40 | + |
| 41 | +### 4. Validate the format of a spanish DNI |
| 42 | + |
| 43 | +Validate the format of a spanish DNI (8 digits and 1 letter). The operation should receive a DNI as parameter and return true if the format is correct, false otherwise. |
| 44 | + |
| 45 | +### 5. From color name to hexadecimal code |
| 46 | + |
| 47 | +Based on existing colors.json file under resources, given the name of the color as path parameter, return the hexadecimal code. If the color is not found, return 404 |
| 48 | + |
| 49 | +Hint: Use TDD. Start by creating the unit test and then implement the code. |
| 50 | + |
| 51 | +### 6. Jokes creator |
| 52 | + |
| 53 | +Create a new operation that call the API https://api.chucknorris.io/jokes/random and return the joke. |
| 54 | + |
| 55 | +### 7. URL parsing |
| 56 | + |
| 57 | +Given a url as query parameter, parse it and return the protocol, host, port, path and query parameters. The response should be in Json format. |
| 58 | + |
| 59 | +### 8. List files and folders |
| 60 | + |
| 61 | +List files and folders under a given path. The path should be a query parameter. The response should be in Json format. |
| 62 | + |
| 63 | +### 9. Word counting |
| 64 | + |
| 65 | +Given the path of a file and count the number of occurrence of a provided word. The path and the word should be query parameters. The response should be in Json format. |
| 66 | + |
| 67 | +### 10. Zipping |
| 68 | + |
| 69 | +Create a zip file with the content of a given folder. The path of the folder should be a query parameter. |
| 70 | + |
| 71 | +### 11. Containerize the application |
| 72 | + |
| 73 | +Use the Dockerfile provided to create a docker image of the application. There are some comments in the Dockerfile that will help you to complete the exercise. |
| 74 | + |
| 75 | +In order to build, run and test the docker image, you can use Copilot as well to generate the commands. |
| 76 | + |
| 77 | +For instance, create a DOCKER.md file where you can store the commands to build, run and test the docker image. You will notice that Copilot will also help you to document your project and commands. |
| 78 | + |
| 79 | +Examples of steps to document: Build the container image, Run the container, Test the container. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
0 commit comments