Spring Boot Web Development Course Guide
Spring Boot Web Development Course Guide
Introduction
Spring Boot is a Java framework. It has been gaining ground against Spring Core.
because this last one had to configure several things and was focused on web development.
While Spring Boot is oriented towards microservices. The difference is that Spring Boot
I configured everything, and you just have to focus on the microservices and set them up.
Spring can work with many technologies (it already includes several, which is what it
dan esa flexibilidad).
Spring offers different projects that are useful depending on what you want to work on:
Microservices
Reactive systems
Cloud Systems
Web Applications
Serverless Applications
Event-based systems
Automated batch tasks
And these are the general ideas that are presented to you on the main page of Spring. There are
many more.
SpringTool Suite
It's like an extension of the Eclipse IDE that provides a more user-friendly environment for the
software development based on spring.
Para crear un proyecto de Spring puedes utilizar el Spring initializer. Esta herramienta se
find in the SpringTool Suite extension. Also on the website:
[Link]
To run your project, you can use the terminal or click RUN in the extension.
HTTP
It is a communication protocol. The function of a communication protocol is to achieve the
communication between two computers. Well, HTTP uses several request methods
to be able to communicate between a computer and a server by passing text, being more
specific hypertext (an HTML example). Generally, the client is the one who will send the
methods and the server will be waiting to dispatch what is requested.
One method of this protocol is GET, which is used for the computer to request a
resource to the server (for example, a webpage).
Another method is HEAD, which is like a GET but does not return the body of the resource.
PUT looks like POST but sends a multipart file, you can send a lot
information departure.
As a developer, you can use them as you please, but following the protocol is
more suitable to avoid future problems in development.
Design patterns
A design pattern is a solution to a software design problem. There were people who
he started to think about the best way to design an application that could
communicate via HTTP or another protocol. Many forms appeared but Spring uses the
Model-View-Controller and dependency injection.
The MVC model implies that the Model is the data, the controller is responsible for
communicate that data to the view; who shows the data. In addition to the view
can interact with the controller.
The Inversion of Control design principle was born to allow the program to invoke or
do whatever we tell you when whatever we have put for you to wait occurs.
Project configuration
For this course, we will use Maven. Which will help us with dependency management.
We will also work with Bootstrap for web page design. If not
we would need to know pure HTML and know exactly what we are modifying.
To add them to the project, you can go to the official Bootstrap page where you
they will indicate what the ways to do it are.
Then the presenter started to create packages where we will keep: controllers,
services, components, entities, repositories and models.
Hello World
Note: If you use VSCode, it is necessary to configure it the way you want to use it for the
Spring Boot extensions.
Install Bootstrap. You can add the dependency in the [Link] or put it yourself.
compiled Bootstrap files. If you want, you can have the source file. I just put it.
in the pom with the Webjars which is like a repository of dependencies for applications or
tools based on JVM (Java Virtual Machine), which is in the repositories of
Maven.
It is not recommended to place libraries or extra elements from sources you do not know or
not official because they may be altered.
NOTE: you can use extra tools for this. If you are working with [Link] you can use the
npm or yarn, you can use composer for PHP, via CDN, if you work with .net you can use
NuGet.
Later we will create the controller, which is a java file that we will create in the folder
(package) of controllers. In spring there are the tags that are represented with the
'@' that indicate how to configure the lines of code presented
in front of them. Several tags can be placed.
So to the new file created, we add the controller tag, which allows us
It will also matter the library it uses (if you installed the extensions correctly).
Later we can indicate that it is a complete route with the label 'RequestMapping'.
We place this label with a "()" and the path it leads to. Then we can place
inside the class functions that can be HTTP methods. We place the tag
“GetMaping” y la función que se ejecutará cuando llegue una petición GET a esa ruta.
Views
With the configurations made and dependencies installed, we can move on to designing the
views, which would be the html files that we will create.
Another note is that Bootstrap 5 (current version on October 19, 2021) does not need jQuery to
funcionar. Así que yo no lo colocaré. Además que al colocarlos por Webjars no es necesario
configure it for thymeleaf.
If you needed to add a global style, you could use Thymeleaf. When adding the
<link> and the <script> do not need to add the th:href="@{folder path relative to the
thymeleaf}” (this position refers to the folders used by thymeleaf) to the link and
th:src="@{folder}" to the script without counting its own href="/path of folder regarding
project.
Add a jumbotron component, but in Bootstrap 5 that component has been removed. So
I only copied the examples that replace that jumbotron.
ALWAYS REMEMBER that you can search for dependencies in the Maven repositories.
IT IS NOT NECESSARY to set the src if you are already doing it with thyme life (th:src) in the
the version I am using of spring boot which is 2.5.5. It doesn't even work for me if I put
the src alone.
Since bootstrap is mostly just classes, you can copy and paste and make small adjustments.
modifications only with classes.
Model
We proceed to reconfigure the controller path.
Now we create a new class called 'post' in the models package. Upon analyzing that
a post we want that needs an image, a title, a description, a date, and an ID
to identify it among all the posts.
After adding those post attributes, we can create their setters and getters. I am
using a library called Lombok, which helps to significantly reduce boilerplate code.
Using @Data, getters, setters and a constructor are created.
Then a list (List<Object>) is created in the controller, which the presenter explains.
what would be better is a repository but that is for another class, and I agree because they are
JPA and Hibernate topics are a completely different matter.
When creating a mini home repository, we can add a for loop in the index with Thymeleaf (no
I know why STS doesn't autocompletes it for me, it's frustrating.
The attribute th:[each|text|src] is added to the tags to replace in the tag that
tribute with what you place, which we will use a variable that was created with the method of
atributos. El cual agregamos un atributo al modelo del MVC el cual podremos usar como
variable for the for and use the attributes of each iteration of the for.
Those Thymeleaf attributes must be reviewed for use. Because they depend on
each tool whatever you want to call it.
ModelAndView
It is necessary for the controller (with the @Controller tag) to add a tag.
@RequestMapping
We added a @GetMapping mapping to the controller so that a method will be executed.
type ModelAndView which is equivalent to the addAttribute used earlier, but
using the MVC pattern. We also added a new class that will store all the
path of the pages.
Right now I am investigating how to add an original repository to a local git repository with ssh
porque si no tendré que crear una key cada vez que necesite hacer push cada cierto
time.
I am also seeing that VSCODE is filling up with extensions and I need to check if
There is a way to have several VSCode instances with different extensions installed.
I fought with the navbar that didn't want to collapse. I was missing a script tag. As already
It does not use jQuery, the Bootstrap script is necessary.
Request Parameters
We are adding another get mapping but now to receive a single instance of a
post model.
I don't know if it's because of the difference in versions, but you can't send a getmapping with the
same name as a template.
A method was added in that method to filter the list of posts by a single id.
Se hizo como [Link]().filter((variable)->{operacionlabdaresultado})
Parameters by path
I just add a @PathVariable.
Post methods and forms
A getmapping was created that can redirect to a form that will send a post request to a
Post Mapping which adds a post to the home mini repository.
The form contains in its attributes: th:action, method, and th:object. The first one is to indicate the
path that will redirect the form, the second to indicate the method and the third to indicate the
object that will be attached to the method.
I am not able to create the form template due to an error that it is not being related.
a bean and I have no idea why.
OK. I solved it by putting the [Link] in the field. Why did I have it in this version?
What to do? I have no idea, in fact, I don't know if it's the most optimal way but oh well. Bye
BEANS
A new model called Users is being created with attributes name and surname.
A new model called Connection with db and url attributes is being created. Setters are added.
y getters
A new package called BEANS was created. In which a new class is created,
createConnection. For a function to be a bean, the @Bean annotation must be added.
In the container, every time a bean is created, it can be called again to avoid the
the need to keep creating instances left and right. These beans are unique.
They serve when you need a specific bean to be inserted where you need it.
Just calling an Autowired.
Because imagine creating a connection every time you need to make a transaction. Well
no.
Components
The microphone doesn't help him much to understand him.
The presenter comments that you can use the entire package path to create a name.
of unique component because you know it will not be repeated unless it is intentional.
That's a good idea but I don't know if it's a good practice.
The controllers are like listeners; they are responsible for listening to events in the project.
Controllers will call another class called services. These services are
they could consider how to handle the data. They can do what you tell them
place, including encapsulating information from different sources.
These services have to retrieve or provide the information to be processed from somewhere. So they
they communicate with components, BEANS, and repositories. The components and Beans
they generally contain encapsulated information. While repositories are
used to make queries to the databases. Repositories need instances
of Entities that are the structure of the tables of the DB of the repositories. (That's what they are for)
services, you can choose the info you get
Services I
Create a package called service and inside it a package that is implementation. The ones that
Those that are in service are interfaces and those that are in implementation are classes.
We can better observe dependency injections and IoC in Spring when the
Autowired is responsible for making everything work.
You can separate the services, they are the qualifiers, and only call the one you need.
It is not advisable to modify existing code. Because you can completely break it.
functionality of your project. Therefore, your project should be open to extending (open)
but closed to modifications (closed).
Decorators are used to achieve those extensions. You can create a file that
have the dependency injections of files that you do not want to modify.
Logs
In the appendix of "Common application properties" there are configurations for the
logging
I am using Lombok's Slf4j. It's the one I used when I started to integrate into
spring projects.
I wouldn't know how to comment on the differences. I only know one because that's the one we worked with. It should be.
to investigate well how to configure it.
Upon investigating, I discovered that Spring Boot uses Logback as the default logging.
Upon doing a little more research, I discovered that it's just a facade. So I should use another one.
to generate the logs correctly.
There is a GREAT DISCUSSION about whether that design pattern is or is not a design pattern,
but that is a topic for another day, although most do not consider it.
It also comments that they are good so that a repository is not created every time it is called.
the bean. Reviewing previous projects I have worked on, the repositories are
used with an interface. I am unaware if there is a different, more effective approach, which
It will need to be reviewed in the future.
Apply the scope to a service and it is understood why you would do it. That way it is already created.
instance and you only call it, which achieves not wasting time creating an instance
porque solamente son funciones.
Upon researching a bit, I discovered that depending on the annotation you use, it can have a
default singleton scope, so it would not be necessary to write it to a service.
The last four (request, session, application, websocket) are used in applications.
web-aware.
When a Singleton Scope is declared, the container creates a single instance of that
BEAN. All the requirements for that BEAN name will return the same object, which
It is cached. Any modification to that object will be reflected to all the
references of the BEAN.
To define a scope, the annotation is written (yes, I have been calling them tags, mistake
my) @Scope("singleton"). You can also use a constant to create the Scope:
@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON).
To define a prototype, the same thing is done but changing singleton for prototype. A
prototype will always return a different instance when referenced.
name of that BEAN.
There was no modification in the code because I find it unnecessary to add code.
redundant.
@Scope 2
It can be added within the @Scope annotation (scopeProxyMode) to indicate that
types of beans can use it. And you can also add the scopeName which is the name
to which it will be added to the scope. These are important because you can save them FOR
SESSION. He comments that you can use them for websockets.
JDBC
Installation
Install a MySQL server.
When adding the dependency to my project, it gives me a warning that it is duplicated, it seems.
that in Spring Boot 2.5.5 the starter is already added. Or it could be due to Spring Devtools
which should also add, but I should look for it better.
Then the MySQL connector for Java is added and surprisingly for me
The configuration also tells me that it is ready. I already checked and apparently in versions
Recent Spring versions already include those connectors in the Maven dependencies.
The main differences that the presenter mentions between JPA and JDBC are that JDBC
it is much lighter since the written queries have to be passed while JPA
it goes like autogenerating because you need to map the objects.
Creating databases
Creating databases with the business logic of the exercise.
Creating Database II
Creating more tables with the business logic of a blog.
I added it just as it is and it throws an exception that doesn't read the driver type. I'm going
to have to investigate what's going on.
I don't know why my IDE marks duplication if it's not taking them into account.
dependencies. aaaaaaaaaaa
Well, it has been added and it works. An instruction was added in the linerunner to add
a permit.
Comment that the Spring Devtools dependency is very useful. Yes, it is. Since the
I added the beginning.
He is adding a command to the line runner to create a Path object from the library.
[Link]. It comments that it has improvements for creating files on a server compared to its
successor [Link]
Then place a try catch to read all the lines of the file to place a
JDBC instruction for it to be executed.
CMS
Models
We have created a new project.
We are creating a package called Model, in which we will create the model files.
the database.
In the first file (Category), there is a relationship. To abstract the relationship we can
create an attribute of the same type as the class. But for now, we create an attribute with the
same type of data that the DB brings, which in this case is an int, but due to the size it
We will pass as a long. Then we create its setters and getters (and I use the annotation of
@Data from lombok). Thus with all the tables of the database.
Repositories
We create a repository for each model. Then it comments that it is necessary to create a class that
implement that interface. In addition to placing the @Repository annotation on them.
we created a JDBC template type attribute for them because that is what we will use to make the queries.
The interface will be the one to receive the instructions and the repository will store all the queries.
to send them to the database.
In the interface, we declare the signature of the methods that will be executed in the repository (a
CRUD). And in the repository we already placed what they are going to do, adding to the method the
annotation @Override which indicates that these will be the instructions if executed
for that moment. The methods to create, update, and delete will be of boolean type that
will return true if executed. While those for reading will return a list of the
models to which the repository belongs but that list will first come from an object
Pageable.
Then the presenter added a base interface repository that has <T> and declared the
CRUD but in the parameters an object of type T is placed. By putting <T> you indicate that it is
a generic interface, which we can use for any type of class.