diff --git a/Home.md b/Home.md index deb72a5..90701fc 100644 --- a/Home.md +++ b/Home.md @@ -1,102 +1,97 @@ ---- -gitea: none -include_toc: true ---- - -This repo is my first attempt to learn `SpringBoot` following [this](https://spring.academy/courses/building-a-rest-api-with-spring-boot/lessons/introduction) tutorial. The setup is Visual Code IDE alongside with [SpringBoot](https://code.visualstudio.com/docs/java/java-spring-boot) plugin. - -# Spring Initializr - -In VsCode press `cmd+shif+p` and type `Spring Initilizr`. Choose: -- Gradle Project -- _SpringBoot version:_ latest (3.1.1) -- _project language:_ Java -- _group id:_ djmil -- _artifact id (aka project name):_ cashcard -- _packaging type:_ jar -- _java version:_ 17 -- _dependencies:_ - - SpringWeb - -Essentially, this will generate a default minimalistic jet functional SpringBoot project. Entry point, aka `main()` can be found in [src/main/java/djmil/cashcard/CashcardApplication.java](http://192.168.8.55:3000/HQLAx/FamilyCashCard/src/branch/main/src/main/java/djmil/cashcard/CashcardApplication.java). To run the application - press `ctrl+F5` or Play button in the top right corner of an editor. - -# TDD - -Different tests can be written at different levels of the system. At each level, there is a balance between the speed of execution, the “cost” to maintain the test, and the confidence it brings to system correctness. This hierarchy is often represented as a “testing pyramid”. - -![Testing pyramid](https://blog.missiondata.com/wp-content/uploads/MD_TestingPyramid2x-1560x1045.png "Testing pyramid") - -## Unit Tests -[[UnitTests]] exercises a small “unit” of the system that is isolated from the rest of the system. They should be simple and speedy. You want a high ratio of Unit Tests in your testing pyramid as they’re key to designing highly cohesive, loosely coupled software. - -## Integration Tests -[[IntegrationTests]] exercise a subset of the system and may exercise groups of units in one test. They are more complicated to write and maintain, and run slower than unit tests. - -## End-to-End Tests -An End-to-End Test exercises the system using the same interface that a user would, such as a web browser. While extremely thorough, End-to-End Tests can be very slow and fragile because they use simulated user interactions in potentially complicated UIs. Implement the smallest number of these tests. - -## TDD Cycle - -Software development teams love to move fast. So how do you go fast forever? By continuously improving and simplifying your code – this is called **refactoring**. One of the only ways you can safely refactor is when you have a trustworthy test suite. Thus, the best time to refactor the code you're currently focusing on is during the TDD cycle. This is called the Red, Green, Refactor development loop: - -1. **Red:** Write a failing test for the desired functionality. -2. **Green:** Implement the simplest thing that can work to make the test pass. -3. **Refactor:** Look for opportunities to simplify, reduce duplication, or otherwise improve the code without changing any behavior—to _refactor._ -4. Repeat! - -# RESTful API - -In a RESTful system, data objects are called Resource Representations. The purpose of a RESTful API is to manage the state of these Resources. The chart below shows details about RESTful CRUD operations of an application. - -|Operation|API Endpoint|HTTP Method|Response Status| -|---|---|---|---| -|**C**reate|`/cashcards`|`POST`|201 (CREATED)| -|**R**ead|`/cashcards/{id}`|`GET`|200 (OK)| -|**U**pdate|`/cashcards/{id}`|`PUT`|204 (NO DATA)| -|**D**elete|`/cashcards/{id}`|`DELETE`|204 (NO DATA)| - -Another common concept associated with REST is the Hypertext Transfer Protocol. In **HTTP**, a caller sends a Request to a URI. A web server receives the request, and routes it to a request handler. The handler creates a Response, which is then sent back to the caller. - -## REST in Spring Boot - -One of the main things Spring does is to configure and instantiate objects. These objects are called *Spring Beans*, and are usually created by Spring (as opposed to using the Java `new` keyword). You can direct Spring to create Beans in several ways. - -> We will annotate a class with a Spring Annotation, which directs Spring to create an instance of the class during Spring’s *Component Scan* phase. This happens at application startup. The Bean is stored in Spring’s `IoC Container`. From here, the bean can be injected into any code that requests it. - -![[Pasted image 20230719102322.png]] - -## @RestController - -In Spring Web, Requests are handled by Controllers. We are going to use the more specific `RestController` annotation. The actual class shall be placed in `src/main/java/djmil/cashcard/CashCardController.java` - -```java -@RestController -public class CashCardController { -} -``` - -That’s all it takes to tell Spring: “create a REST Controller”. The Controller gets injected into Spring Web, which routes API requests (handled by the Controller) with help of [[GET#@GetMapping]] annotation to the correct method. - -## Get -**** -In [[GET]] requests, the body is empty. So, the request to read the Cash Card with an id of 123 would be: - -``` -Request: - Method: GET - URL: http://cashcard.example.com/cashcards/123 - Body: (empty) -``` - -The response to a successful Read request has a body containing the JSON representation of the Resource which was requested, with a Response Status Code of 200 (OK). So the response to the above Read request would look like this: - -``` -Response: - Status Code: 200 - Body: - { - "id": 123, - "amount": 25.00 - } -``` - +This repo is my first attempt to learn `SpringBoot` following [this](https://spring.academy/courses/building-a-rest-api-with-spring-boot/lessons/introduction) tutorial. The setup is Visual Code IDE alongside with [SpringBoot](https://code.visualstudio.com/docs/java/java-spring-boot) plugin. + +# Spring Initializr + +In VsCode press `cmd+shif+p` and type `Spring Initilizr`. Choose: +- Gradle Project +- _SpringBoot version:_ latest (3.1.1) +- _project language:_ Java +- _group id:_ djmil +- _artifact id (aka project name):_ cashcard +- _packaging type:_ jar +- _java version:_ 17 +- _dependencies:_ + - SpringWeb + +Essentially, this will generate a default minimalistic jet functional SpringBoot project. Entry point, aka `main()` can be found in [src/main/java/djmil/cashcard/CashcardApplication.java](http://192.168.8.55:3000/HQLAx/FamilyCashCard/src/branch/main/src/main/java/djmil/cashcard/CashcardApplication.java). To run the application - press `ctrl+F5` or Play button in the top right corner of an editor. + +# TDD + +Different tests can be written at different levels of the system. At each level, there is a balance between the speed of execution, the “cost” to maintain the test, and the confidence it brings to system correctness. This hierarchy is often represented as a “testing pyramid”. + +![Testing pyramid](https://blog.missiondata.com/wp-content/uploads/MD_TestingPyramid2x-1560x1045.png "Testing pyramid") + +## Unit Tests +[[UnitTests]] exercises a small “unit” of the system that is isolated from the rest of the system. They should be simple and speedy. You want a high ratio of Unit Tests in your testing pyramid as they’re key to designing highly cohesive, loosely coupled software. + +## Integration Tests +[[IntegrationTests]] exercise a subset of the system and may exercise groups of units in one test. They are more complicated to write and maintain, and run slower than unit tests. + +## End-to-End Tests +An End-to-End Test exercises the system using the same interface that a user would, such as a web browser. While extremely thorough, End-to-End Tests can be very slow and fragile because they use simulated user interactions in potentially complicated UIs. Implement the smallest number of these tests. + +## TDD Cycle + +Software development teams love to move fast. So how do you go fast forever? By continuously improving and simplifying your code – this is called **refactoring**. One of the only ways you can safely refactor is when you have a trustworthy test suite. Thus, the best time to refactor the code you're currently focusing on is during the TDD cycle. This is called the Red, Green, Refactor development loop: + +1. **Red:** Write a failing test for the desired functionality. +2. **Green:** Implement the simplest thing that can work to make the test pass. +3. **Refactor:** Look for opportunities to simplify, reduce duplication, or otherwise improve the code without changing any behavior—to _refactor._ +4. Repeat! + +# RESTful API + +In a RESTful system, data objects are called Resource Representations. The purpose of a RESTful API is to manage the state of these Resources. The chart below shows details about RESTful CRUD operations of an application. + +|Operation|API Endpoint|HTTP Method|Response Status| +|---|---|---|---| +|**C**reate|`/cashcards`|`POST`|201 (CREATED)| +|**R**ead|`/cashcards/{id}`|`GET`|200 (OK)| +|**U**pdate|`/cashcards/{id}`|`PUT`|204 (NO DATA)| +|**D**elete|`/cashcards/{id}`|`DELETE`|204 (NO DATA)| + +Another common concept associated with REST is the Hypertext Transfer Protocol. In **HTTP**, a caller sends a Request to a URI. A web server receives the request, and routes it to a request handler. The handler creates a Response, which is then sent back to the caller. + +## REST in Spring Boot + +One of the main things Spring does is to configure and instantiate objects. These objects are called *Spring Beans*, and are usually created by Spring (as opposed to using the Java `new` keyword). You can direct Spring to create Beans in several ways. + +> We will annotate a class with a Spring Annotation, which directs Spring to create an instance of the class during Spring’s *Component Scan* phase. This happens at application startup. The Bean is stored in Spring’s `IoC Container`. From here, the bean can be injected into any code that requests it. + +![[Pasted image 20230719102322.png]] + +## @RestController + +In Spring Web, Requests are handled by Controllers. We are going to use the more specific `RestController` annotation. The actual class shall be placed in `src/main/java/djmil/cashcard/CashCardController.java` + +```java +@RestController +public class CashCardController { +} +``` + +That’s all it takes to tell Spring: “create a REST Controller”. The Controller gets injected into Spring Web, which routes API requests (handled by the Controller) with help of [[GET#@GetMapping]] annotation to the correct method. + +## Get +**** +In [[GET]] requests, the body is empty. So, the request to read the Cash Card with an id of 123 would be: + +``` +Request: + Method: GET + URL: http://cashcard.example.com/cashcards/123 + Body: (empty) +``` + +The response to a successful Read request has a body containing the JSON representation of the Resource which was requested, with a Response Status Code of 200 (OK). So the response to the above Read request would look like this: + +``` +Response: + Status Code: 200 + Body: + { + "id": 123, + "amount": 25.00 + } +``` +