Spring Course 2024v3
Spring Course 2024v3
Spring
I. Introduction
Spring is a Java framework for building web applications.
- Spring Boot: Takes an opinionated view of building Spring applications and gets you up and
running as quickly as possible.
- Spring Framework: Provides core support for dependency injection, transaction management,
web apps, data access, messaging, and more.
- Spring Data: Provides a consistent approach to data access – relational, non-relational, map-
reduce, and beyond.
- Spring Cloud: Provides a set of tools for common patterns in distributed systems. Useful for
building and deploying microservices.
- Spring Cloud Data Flow: Provides an orchestration service for composable data microservice
applications on modern runtimes.
- Spring Security: Protects your application with comprehensive and extensible authentication
and authorization support.
- Spring Authorization Server: Provides a secure, light-weight, and customizable foundation for
building OpenID Connect 1.0 Identity Providers and OAuth2 Authorization Server products.
- Spring for GraphQL: Spring for GraphQL provides support for Spring applications built on
GraphQL Java.
- Spring Session: Provides an API and implementations for managing a user’s session information.
- Spring Integration: Supports the well-known Enterprise Integration Patterns through lightweight
messaging and declarative adapters.
- Spring Web Services: Facilitates the development of contract-first SOAP web services.
See: https://spring.io/projects
The term "Spring" means different things in different contexts. It can be used to refer to the
Spring Framework project itself. Most often, when people say "Spring", they mean the entire
family of projects. (link: https://docs.spring.io/spring-framework/reference/overview.html)
1
II. Spring Framework
The current version of Spring Framework is 6.1.3. As of Spring Framework 6.0, Spring requires Java 17+.
The Spring Framework uses Beans. It supports the Dependency Injection and Common Annotations.
Features:
Core technologies: dependency injection, events, resources, i18n, validation, data binding, type
conversion, SpEL, AOP.
Integration: remoting, JMS, JCA, JMX, email, tasks, scheduling, cache and observability.
1. Annotations
@Entity
The at sign character (@) indicates to the compiler that what follows is an annotation. In the following
example, the annotation's name is Override:
@Override
void mySuperMethod() {
}
2
The annotation can include elements, which can be named or unnamed, and there are values for those
elements:
or
@SuppressWarnings(value = "unchecked")
void myMethod() {
}
If there is just one element named value, then the name can be omitted, as in:
@SuppressWarnings("unchecked")
void myMethod() {
}
If the annotation has no elements, then the parentheses can be omitted, as shown in the previous
@Override example.
The annotation type can be one of the types that are already defined. In the previous examples,
Override and SuppressWarnings are predefined Java annotations. It is also possible to define your own
annotation type. The Author and Ebook annotations in the previous example are custom annotations.
Annotations can be applied to declarations: declarations of classes, fields, methods, and other program
elements.
3
2. Beans
@RestController
public class HomeController {
@GetMapping
public String index() {
return "hello world";
}
}
Example:
@SpringBootApplication
public class WebapiApplication {
public static void main(String[] args) {
SpringApplication.run(WebapiApplication.class, args);
}
}
3. Dependency injection
Dependency injection (DI) is a process where objects define their dependencies (that is, the other
objects they need). The container then injects those dependencies when it creates the bean.
4
A class can define its dependencies in different ways:
- constructor arguments
- arguments to a method, or
- fields annotated with @Autowired
The following example shows a class that can be dependency-injected with @Autowired annotation:
Spring is mainly used to create REST Web Services (RESTful Web API). It lets you create special
@RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to
HTTP by using @RequestMapping annotations.
The following code shows a typical @RestController that serves JSON data:
5
@RestController
@RequestMapping("/users")
public class MyRestController {
private final UserRepository userRepository;
@GetMapping
public List<User> getUsers() {
return this.userRepository.findAll();
}
@GetMapping("/{userId}")
public User getUser(@PathVariable Long userId) {
return this.userRepository.findById(userId).get();
}
@PostMapping
public void createUser(@RequestBody User user) {
// create user
}
@PutMapping("/{userId}")
public User updateUser(@PathVariable Long userId, @RequestBody User user)
{
// update user
}
@DeleteMapping("/{userId}")
public void deleteUser(@PathVariable Long userId) {
this.userRepository.deleteById(userId);
}
}
5. Spring MVC
The Spring Web MVC framework (often referred to as “Spring MVC”) is a rich “model view controller”
web framework.
Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP
requests.
6
Spring MVC allows us to serve dynamic HTML content. It supports a variety of templating technologies,
including:
- Thymeleaf
- JSP
- FreeMarker
- Groovy
- Mustache
If possible, JSPs should be avoided. There are several known limitations when using them with
embedded servlet containers.
When you use one of these templating engines with the default configuration, your templates are
picked up automatically from src/main/resources/templates.
Spring Boot makes it easy to create Spring based Applications that you can "just run".
Features
Getting Started
7
1. Create new project
The easiest way to create a new project is using Spring initializr: https://start.spring.io/
We choose Maven as the project building tool, and Java as the programming language
- Group : name of the application owner (or his reversed domain name)
- Artifact: the application name (Artifact = Name)
Generate the Zip file, extract it and open it using your IDE
8
2. Project Structure
Required tools:
- IDE
o
IntelliJ IDE
o
Eclipse IDE for Enterprise Java and Web Developers:
https://www.eclipse.org/downloads/packages/
o VS Code + Java Extension Pack + Spring Boot Extension Pack
- Postman
9
TP 1
10
Chapter 2. Spring Web
I. Controllers
Link : https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller.html
1. Declaration
@RestController
public class HelloController {
@GetMapping("/hello")
public String handle() {
return "Hello World!";
}
}
In the preceding example, the method is accessible using the HTTP GET method at the URL “/hello” and
returns a String.
2. API Endpoint
An API endpoint is a URL that acts as the point of contact between a client and a server:
- Clients send requests to API endpoints in order to access the API’s functionality and data.
- A typical REST API has many endpoints that correspond to its available resources.
11
For example, a REST API for blogging application might have the following URLs endpoints:
3. Mapping Requests
You can use the @RequestMapping annotation to map requests to controller methods. You can use it at
the class level to express shared mappings or at the method level to express a specific endpoint
mapping.
The following example creates an endpoint accessible using the HTTP GET method at the URL “/
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
The shortcuts are mainly used with controller methods while @RequestMapping is usually needed at
the class level to express shared mappings.
12
The following example has class and method level mappings:
@RestController
@RequestMapping("/books")
public class BooksController {
@GetMapping
public String getBooks() {
return "getBooks";
}
@GetMapping("list")
public String getBooks2() {
return "getBooks2";
}
The method “getBooks2” is accessible at the URL “/books/list” (URL “/books/list/” is not defined)
The method “getBooks3” is accessible at the URL “/books/get”, “/books/getAll” and “/books/getAll/”
Methods annotated with @RequestMapping (and its shortcuts) can be mapped using URL patterns.
Example: "/projects/{project}/versions" - match a URL segment (project) and capture it as a variable.
@GetMapping("/owners/{ownerId}/pets/{petId}")
public String findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
// ...
}
4. Handler Methods
Controller methods annotated with @RequestMapping (or its shortcuts) are called handler methods.
13
The method arguments are called the method signature.
Handler methods have a flexible signature and can choose from a range of supported arguments and
return values.
a. Method Arguments
The complete list of supported controller method arguments is available at this link.
/data/list?abc=15&test=bonjour&xyz=2024
Example 1:
@RestController
@RequestMapping("/books")
public class BooksController {
@GetMapping
public String getBooks(
@RequestParam String author,
@RequestParam(defaultValue = "all") String category) {
return "Get Books of " + author + " - category : " + category;
}
}
14
The response of a GET request to http://localhost/books?author=Ali&category=Science is: Get Books
of Ali - category : Science
The response of a GET request to http://localhost/books?author=Ali is: Get Books of Ali - category : all
Example 2:
@RestController
@RequestMapping("/books")
public class BooksController {
@PutMapping("{id}")
public String updateBook(
@PathVariable int id,
@RequestBody Book book) {
return "id=" + id + " - title: " + book.getTitle();
}
}
15
b. Return Values
The complete list of supported controller method return values is available at this link. The next table
describes some of them:
Example 1:
@GetMapping("/titles")
public List<String> getTitles() {
List<String> titles = new ArrayList<String>();
titles.add("title1");
titles.add("title2");
titles.add("title3");
return titles;
}
Response:
[
"title1",
"title2",
"title3"
]
16
Example 2:
@GetMapping("/books/best")
public Book getBestBook() {
Book book = new Book();
book.setTitle("Spring Boot Guide");
book.setAuthor("Ali Dridi");
book.setIsbn("45874584211");
book.setPrice(78.9);
return book;
}
Response:
{
"title": "Spring Boot Guide",
"author": "Ali Dridi",
"isbn": "45874584211",
"price": 78.9
}
Example 3:
@GetMapping("{id}")
public ResponseEntity<Object> getBook(@PathVariable int id) {
Book book = new Book();
book.setTitle("Spring Boot Guide");
book.setAuthor("Ali Dridi");
book.setIsbn("45874584211");
book.setPrice(78.9);
Response to http://localhost:8080/books/0 :
17
c. Validation
Link: https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-
validation.html
Method parameters such as @RequestBody and @RequestPart do perform validation if annotated with
Jakarta’s @Valid or Spring’s @Validated annotation, and raise MethodArgumentNotValidException in
case of validation errors. If you want to handle the errors in the controller method instead, you can
declare an Errors or BindingResult method parameter immediately after the validated parameter.
- Add the “Validation” dependency to the project. It can be added while creating a new
project using Spring initializr, or you can add the following tag to “pom.xml”:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- Create a new Model and annotate its fields using the appropriate annotations, example:
@Min(0)
private double price;
- Add the @Valid annotation to method parameters such as @RequestBody. This will raise
MethodArgumentNotValidException in case of validation errors.
18
- To handle the errors in the controller method instead, you can declare a BindingResult
method parameter immediately after the validated parameter, example:
@PostMapping
public ResponseEntity<Object> createBook(
@Valid @RequestBody Book book,
BindingResult result
) {
if (result.hasErrors()) {
var errorsList = result.getAllErrors();
var errorsMap = new HashMap<String, String>();
return ResponseEntity.badRequest().body(errorsMap);
}
return ResponseEntity.ok(book);
}
19
TP 2
1) Add the “Validation” dependency to the “webapi” project by adding the following xml tag to
“pom.xml”:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2) Create the “models” package and add 2 models in this package: Product and ProductDto.
ProductDto has the following fields: String name, String brand, String category, double price,
String description
All these fields are private and required. Generate the getters and setters using the IDE. The
Product model has the same fields in addition to the “int id” and “Date createdAt”.