DTOs & Validation
អានប្រមាណ 2 នាទី · ធ្វើបច្ចុប្បន្នភាពនៅ 31 តុលា 2026
ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។
Table of Contents
- Why Not Accept a
Map - DTOs as Records
- Adding Validation
- Using Them in the Controller and Service
- Trying It
- Beginner Pitfalls
1. Why Not Accept a Map
Part 4's create takes a Map<String, Object> and casts. A missing title becomes a
null inside the app, a number where text was expected throws a ClassCastException
(a 500), and nobody reading the signature can tell what a valid body looks like.
The fix is a DTO (Data Transfer Object): a class whose only job is to describe the
shape of data crossing the HTTP boundary.
2. DTOs as Records
In Java a record is the natural DTO — immutable, tiny, and the annotations go
straight on the components. Make a dto sub-package inside tasks:
// src/main/java/com/example/taskapi/tasks/dto/CreateTaskRequest.java
package com.example.taskapi.tasks.dto;
public record CreateTaskRequest(String title, String description, Boolean done) {
}
// src/main/java/com/example/taskapi/tasks/dto/UpdateTaskRequest.java
package com.example.taskapi.tasks.dto;
/** Every field is optional: a field left out of the JSON stays as it is. */
public record UpdateTaskRequest(String title, String description, Boolean done) {
}
done is Boolean (the object type), not boolean, so "not provided" is null
rather than silently false.
3. Adding Validation
Add the validation starter to the dependencies { } block of build.gradle:
implementation 'org.springframework.boot:spring-boot-starter-validation'
Then declare the rules directly on the record components:
// CreateTaskRequest.java
public record CreateTaskRequest(
@NotBlank @Size(max = 120) String title,
String description,
Boolean done) {
}
// UpdateTaskRequest.java
public record UpdateTaskRequest(
@Size(max = 120) @Pattern(regexp = ".*\\S.*", message = "must not be blank") String title,
String description,
Boolean done) {
}
| Annotation | Rule |
|---|---|
@NotBlank | Not null, not empty, not only spaces |
@Size(max = 120) | At most 120 characters |
@Pattern(...) | Must match a regex — here, "contains a non-space character", so an omitted title is allowed on update but an empty one is not |
(Imports come from jakarta.validation.constraints.)
4. Using Them in the Controller and Service
Rules do nothing until something checks them: @Valid on the controller
parameter tells Spring to validate the body before the method runs.
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Task create(@Valid @RequestBody CreateTaskRequest request) {
return taskService.create(request.title(), request.description(),
Boolean.TRUE.equals(request.done()));
}
@PatchMapping("/{id}")
public Task update(@PathVariable Long id, @Valid @RequestBody UpdateTaskRequest request) {
return taskService.update(id, request).orElse(null); // proper 404 in Part 7
}
Add the matching update to TaskService, applying only the fields that were sent:
public Optional<Task> update(Long id, UpdateTaskRequest request) {
return findOne(id).map(task -> {
if (request.title() != null) task.setTitle(request.title());
if (request.description() != null) task.setDescription(request.description());
if (request.done() != null) task.setDone(request.done());
return task;
});
}
5. Trying It
curl -i -X POST http://localhost:8080/tasks -H "Content-Type: application/json" \
-d '{"title":" "}'
curl -i -X POST http://localhost:8080/tasks -H "Content-Type: application/json" \
-d '{"title":"Valid task"}'
curl -X PATCH http://localhost:8080/tasks/1 -H "Content-Type: application/json" \
-d '{"done":true}'
The blank title is rejected with 400 Bad Request before TaskService is ever
called; the valid one returns 201; the patch changes only done.
Note: out of the box the
400body is short. Listing each failing field in a consistent error shape takes an exception handler — a topic for the full course beyond this crash course.
6. Beginner Pitfalls
- Forgetting
@Valid. The annotations on the record are only declarations; without@Validon the parameter, invalid input sails straight through. - Using
booleanfor an optional field. A primitive can't benull, so an omitted field becomesfalseand an update would silently overwrite it. - Reusing one DTO for create and update. Their rules differ — create requires a title, update makes everything optional — so keep two small records.
Official docs:
តើជំពូកនេះមានប្រយោជន៍ទេ?
ទទួលមេរៀនថ្មីៗ
ចូលរួមឆានែល Telegram ដើម្បីដឹងនៅពេលមានជំពូក ឬស៊េរីថ្មី ឬតាមដានតាម RSS។
ទិញកាហ្វេជូនខ្ញុំ
ចូលចិត្តមេរៀននេះមែនទេ? សូមពិចារណាទិញកាហ្វេជូនខ្ញុំមួយកែវ ដើម្បីគាំទ្រការបង្កើតមាតិកាឥតគិតថ្លៃបន្ថែមទៀត!