Skip to content

Commit 2d9a4b4

Browse files
committed
Validate objects for PUT and POST methods
1 parent 0cee19d commit 2d9a4b4

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
<artifactId>spring-boot-devtools</artifactId>
4444
</dependency>
4545

46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-validation</artifactId>
49+
</dependency>
4650
</dependencies>
4751

4852
<build>

src/main/java/com/dsan/springrestapicourse/dto/CategoryDTO.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
import java.io.Serializable;
44

5+
import javax.validation.constraints.NotEmpty;
6+
7+
import org.hibernate.validator.constraints.Length;
8+
59
import com.dsan.springrestapicourse.domain.Category;
610

711
public class CategoryDTO implements Serializable {
812
private static final long serialVersionUID = 1L;
913

1014
private Integer id;
15+
16+
@NotEmpty
17+
@Length(min = 5, max = 80)
1118
private String name;
1219

1320
public CategoryDTO() {
1421

1522
}
16-
23+
1724
public CategoryDTO(Category category) {
1825
id = category.getId();
1926
name = category.getName();
2027
}
21-
2228

2329
public Integer getId() {
2430
return id;
@@ -36,4 +42,8 @@ public void setName(String name) {
3642
this.name = name;
3743
}
3844

45+
public Category toCategory() {
46+
return new Category(id, name);
47+
}
48+
3949
}

src/main/java/com/dsan/springrestapicourse/resources/CategoryResource.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55
import java.util.stream.Collectors;
66

7+
import javax.validation.Valid;
8+
79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.data.domain.Page;
911
import org.springframework.http.ResponseEntity;
@@ -36,17 +38,17 @@ public ResponseEntity<Category> find(@PathVariable Integer id) {
3638
}
3739

3840
@PostMapping
39-
public ResponseEntity<Void> insert(@RequestBody Category category) {
40-
category = categoryService.insert(category);
41-
42-
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(category.getId())
43-
.toUri();
41+
public ResponseEntity<Void> insert(@Valid @RequestBody CategoryDTO categoryDto) {
42+
Category category = categoryService.insert(categoryDto.toCategory());
43+
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").
44+
buildAndExpand(category.getId()).toUri();
4445

4546
return ResponseEntity.created(uri).build();
4647
}
4748

4849
@PutMapping("/{id}")
49-
public ResponseEntity<Void> update(@RequestBody Category category, @PathVariable Integer id) {
50+
public ResponseEntity<Void> update(@Valid @RequestBody CategoryDTO categoryDto, @PathVariable Integer id) {
51+
Category category = categoryDto.toCategory();
5052
category.setId(id);
5153
category = categoryService.update(category);
5254
return ResponseEntity.noContent().build();
@@ -66,11 +68,10 @@ public ResponseEntity<List<CategoryDTO>> findAll() {
6668
}
6769

6870
@GetMapping("/page")
69-
public ResponseEntity<Page<CategoryDTO>> findPage(
70-
@RequestParam(name = "page", defaultValue = "0") Integer page,
71-
@RequestParam(name = "linesPerPage", defaultValue = "24")Integer linesPerPages,
72-
@RequestParam(name = "orderBy", defaultValue = "name")String orderBy,
73-
@RequestParam(name = "direction", defaultValue = "ASC")String direction) {
71+
public ResponseEntity<Page<CategoryDTO>> findPage(@RequestParam(name = "page", defaultValue = "0") Integer page,
72+
@RequestParam(name = "linesPerPage", defaultValue = "24") Integer linesPerPages,
73+
@RequestParam(name = "orderBy", defaultValue = "name") String orderBy,
74+
@RequestParam(name = "direction", defaultValue = "ASC") String direction) {
7475
Page<Category> listCategory = categoryService.findPage(page, linesPerPages, orderBy, direction);
7576
Page<CategoryDTO> listDTO = listCategory.map(e -> new CategoryDTO(e));
7677
return ResponseEntity.ok().body(listDTO);

src/main/java/com/dsan/springrestapicourse/services/CategoryServiceImpl.java

-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,4 @@ public Page<Category> findPage(Integer page, Integer linesPerPages, String order
6262
PageRequest pageRequest = PageRequest.of(page, linesPerPages, Direction.valueOf(direction), orderBy);
6363
return categoryRepository.findAll(pageRequest);
6464
}
65-
6665
}

0 commit comments

Comments
 (0)