-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpring Boot Basics.txt
235 lines (149 loc) · 7.67 KB
/
Spring Boot Basics.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Things to learn more about:
- beans
- component scanning
- class path
Problems w/ Traditional Spring
-----------------------------------------
- keeping up with dependecies, module availability, and version compatibablity is burdensome for us
- deployment has to be done to external web containers/servers
Advantages of SpringBoot
-----------------------------------
[ AutoConfiguration ]
- sets up/configures everything we need within our app
- don't have to deal with xml, java, or annotation based configuration anymore
- if we use the web or "mvc" module, SpringBoot automatically configures the dispatcher servlet and takes care of request mapping for us
- if we use the "orm" module, we no longer have to configure the DataSource or TransactionManager
[ SpringBoot Starters ]
- take care of module availability and version compatiability for us;
- every project has a spring-boot-starter-parent that contains all of the version information for all of the libraries that will be contained in our project
[ Embedded Servlet Containers ]
- provides embedded web containers for us to deploy to; default is Tomcat but also supports Jetty and Undertow
[ SpringBoot Actuators ]
- provides utility routes/endpoints that we can consume in our app (autoconfig, mappings, info, health, metrics)
- can quickly intergrate 3rd party apps (prometheus) using these endpoints
Configuring JPA
Deploying/Running A SpringBoot Application
-----------------------------------------------------------
[ Inside SpringBoot - Embeded Tomcat Server ]
[ Outsude of SpringBoot - Standalone JAR ]
- in order to run the application on any system we want we must compile our application and get a "jar" artifact. This is the articfact that we will pass to java to run on any remote system that has java installed
// Process
Execute maven "install" goal
this compiles out project and spits our a fully packaged jar file for us to execute
Run java and reference the jar file
java -jar <name_of_project>.jar
This will cause our app to run just like it did when we ran it inside of Spring Boot
[ Using JAR Layers - layers.idx - Docker ]
- build an optimized container image from the contents (layers) of the jar file
- helps slim down the overall size of a Springboot based images; instead of copying "fat" jar
// JAR Layers
Dependencies - all dependencies whose version does NOT contain SNAPSHOT
Spring-Boot-Loader - springboot loader classes
Snapshot-Dependencies - all dependencies whose version DOES contain SNAPSHOT
Application - application classes, resources
// Process
java -Djarmode=layertools jar <jar> list
java -Djarmode=layertools -jar <jar> extract -- destination layers // performs an extraction to directory called "layers"
Logging
----------
In Spring Boot all of the starter packages are included for us to log. The default implementation is sl4j.
Create an instance of a Logger in your class
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger LOGGER = LoggerFactory.getLogger(ProductController.class);
Set the log level for the application in the application.properties file
log.level=warn
log.level.org.org.springframework=error // log for a particular framework in your app
log.com.com.lowsandbox.restapi.controllers.ProductRestController=error // log a particular package
Health Checks + Metrics
--------------------------------
Spring Boot uses "actuators" to provide us with REST Endpoints that we can consume that provides us rich telemtry about
our app
Add the actuator starter pack to your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Check the endpoint that is exposed
2021-05-24 08:15:46.038 INFO 39728 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' // log line relevant to actuator running
http://localhost:8080/api/actuator
Response:
{
"_links": {
"self": {
"href": "http://localhost:8080/api/actuator",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/api/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:8080/api/actuator/health",
"templated": false
}
}
}
Update application.properties to expose more health details
management.endpoint.health.show-details=always
re-run application and check the /health endpoint again
Add the following line to expose ALL of the actuator endpoints
management.endpoints.web.exposure.include=*
Visit http://localhost:8080/api/actuator/metrics endpoint
http://localhost:8080/api/actuator/metrics/<metric_name> // for more details
Enabling Security On Endpoints/Routes
----------------------------------------------------
It is very simple to protect routes in Spring Boot
Add security starter pack in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Look for line in the console output
Using generated security password: c8abbda3-9d80-4a11-9ee7-a96589abfb48
When you attempted to access any routes, you will be met with a "Login" page
The username is "user"
Password is the generated string you see in the output above
Thymeleaf Templates (Server-Side Rendering -- MVC)
-----------------------------------------------------------------------
Thymeleaf is the new standard template engine for rendering static and dynamic html pages. In the past, JSP (Java Server Pages) was the standard, but the language has since labeled this standard deprecated in favor of Thymeleaf
Tymeleaf is very similar to other language specific template engines such as Razor (C#), and Django (Python). These engines make implementing the MVC architectures in each of the respective languages easier
MVC Controller
--------------------
When creating a controller it is important to note the diffierence between the annotations @RestController and @Controller
@RestController - used specifically to declare that your controller will function as a RESTful service providing JSON or XML to it's clients
@Controller - used specifically to declare that your controller will function in a MVC fashion providing views to it's clients in the form of HTML
Create a controller class
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello"; // name of the "template" you will return to the browser
}
}
Create a hello.html template file
<html xmlns:th="http://www.thymeleaf.org">
<title>Thymeleaf</title>
<body>
<b>Hello Thymeleaf!</b>
</body>
</html>
Passing Data To A Template
----------------------------------------
Define a new function that will handle the passing data from the model to the view
@GetMapping("sendData")
public ModelAndView sendData() {
ModelAndView mav = new ModelAndView("data"); // name of the template
mav.addObject("message", "Getting started with Tymeleaf!"); // inject data into the view
return mav; // return the "data.html" template to the client
}
Disabling Caching In Thymeleaf
------------------------------------------
By default, thymeleaf caches responses on the client side so that sometime when changes are made to the template, the client will not pick up those new changes due to caching.
Disabe caching my adding this to application.properties
spring.thymeleaf.cache=false
Messaging in Java
-------------------------
MOM - Message Oriented Middleware (ActiveMQ, RabbitMQ, AWS SQS)
Sender -----> MOM (Messaging Server) <------ Receiver
(App1) (App2)