Difference between revisions of "Spring 5 training (Spring guru) - personal notes"

(springMVC)
(springMVC)
Line 95: Line 95:
 
# ''Dispatcher'' '''receives''' the request
 
# ''Dispatcher'' '''receives''' the request
 
# ''Dispatcher'' asks the ''Handler'' '''WHO should take care of the request'''? i.e:  
 
# ''Dispatcher'' asks the ''Handler'' '''WHO should take care of the request'''? i.e:  
** What is the ''Controller'' for the requested endpoint?
+
#* What is the ''Controller'' for the requested endpoint?
** Is there any ''java Method'' that match the given HTTP request type (GET, POST, PUT, ..) + arguments?
+
#* Is there any ''java Method'' that match the given HTTP request type (GET, POST, PUT, ..) + arguments?
 
# ''Dispatcher'' '''forwards the query''' to the ''Controller''
 
# ''Dispatcher'' '''forwards the query''' to the ''Controller''
** Controller calls a ''Service'' to process the query, interact with the database and other systems.
+
#* Controller calls a ''Service'' to process the query, interact with the database and other systems.
** Once the ''Service'' processing is complete, it returns corresponding ''data'' to the ''Controller''
+
#* Once the ''Service'' processing is complete, it returns corresponding ''data'' to the ''Controller''
** The ''Controller'' will convert the data into a DTO (communication ''Model'' (''pojo'')  
+
#* The ''Controller'' will convert the data into a DTO (communication ''Model'' (''pojo'')  
** ''Controller'' sends the ''pojo'' back to the ''Dispatcher''
+
#* ''Controller'' sends the ''pojo'' back to the ''Dispatcher''
 
# '''Data disposal''': The ''Dispatcher'' disposes of the data. i.e:
 
# '''Data disposal''': The ''Dispatcher'' disposes of the data. i.e:
** Send back the data directly to the client [HTTP REST calls]
+
#* Send back the data directly to the client [HTTP REST calls]
** Forward the data to a ''View'' for rending
+
#* Forward the data to a ''View'' for rending
  
 
If we have to render the data:
 
If we have to render the data:

Revision as of 18:05, 9 March 2018


I recently (2018) subscribe to an online training related to Spring 5 on Udemy. This page is a sum-up of my personal notes related to Spring 5.


Links

  • Spring initializr - to generate a quick start project based on the libraries you choose


Very simple web-application

  • Go to Spring initializr
  • Select
    • web > web
    • SQL > JPA
    • SQL > H2
    • Templates > Thymeleaf
    • Ops > actuator


H2 hint

When using H2 on a web application, don't forget to enable the console! Edit application.properties as follow:

spring.h2.console.enabled=true
# replace "spring5webapp" by your own application
spring.datasource.url=jdbc:h2:mem:spring5webapp;DB_CLOSE_ON_EXIT=FALSE

Then you can reach the console at http://localhost:8080/h2-console


Hibernate

General notes

Hibernate recommends to implement hashCode() and equals() on all JPA entities. They also recommend to use a particular Business ID when possible, if not then use the ID. Only the key fields should be in these methods.


How to run project

Spring boot provides some out-of-the-box tools.

To run the application:

# Using the maven installation (outside the project)
mvn spring-boot:run

# Using the embedded tool
./mvnw spring-boot:run


IntelliJ configuration

Plugins

Useful plugings:

  • File > Settings > Plugins > Browse repositories
    • .ignore
    • Ideolog
    • Sonarlint
    • Save actions
    • GenerateSerialVersion

Enable toolbars! View > Toolbar


Import optimization

see https://stackoverflow.com/questions/11704327/how-to-optimize-imports-automatically-after-each-save-in-intellij-idea


IntelliJ IDEA offers 2 options:

  • Settings | Editor | General | Auto Import > Optimize imports on the fly
  • Commit Project dialog > Optimize imports option


Spring 5

springMVC

Implementation of MVC concept in Spring:

MVC pattern in Spring (credentials: Spring Guru, UDEMY)

Here is a short description of the schema - as I understood it + based on my experience with Spring so far (v2 to v4):

  1. Client submits a request to the server (HTTP request)
  2. Dispatcher receives the request
  3. Dispatcher asks the Handler WHO should take care of the request? i.e:
    • What is the Controller for the requested endpoint?
    • Is there any java Method that match the given HTTP request type (GET, POST, PUT, ..) + arguments?
  4. Dispatcher forwards the query to the Controller
    • Controller calls a Service to process the query, interact with the database and other systems.
    • Once the Service processing is complete, it returns corresponding data to the Controller
    • The Controller will convert the data into a DTO (communication Model (pojo)
    • Controller sends the pojo back to the Dispatcher
  5. Data disposal: The Dispatcher disposes of the data. i.e:
    • Send back the data directly to the client [HTTP REST calls]
    • Forward the data to a View for rending

If we have to render the data:

  1. View generates the page thanks to UI technology (server side) such as JSP, JSF or Themeleaf to generate HTML / Javascript / CSS
  2. View sends back generated content to Dispatcher
  3. Dispatcher forwards content to the Client