From 268b798e7b91557f415559e95e4e868d3ab21c03 Mon Sep 17 00:00:00 2001 From: Alex B Date: Wed, 25 Oct 2023 11:58:45 -0300 Subject: [PATCH] Adding validation (fixed) --- pom.xml | 14 +++++++++++++ .../blog/controller/PostController.java | 19 ++++++++++++++++-- .../java/com/example/blog/model/Post.java | 4 +++- .../blog/controller/PostController.class | Bin 3335 -> 5360 bytes .../classes/com/example/blog/model/Post.class | Bin 1430 -> 1633 bytes 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 52178b7..5c2b33f 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,20 @@ org.postgresql postgresql + + org.springframework.boot + spring-boot-starter-validation + + + jakarta.validation + jakarta.validation-api + 2.0.2 + + + org.springframework + spring-web + 5.3.10 + diff --git a/src/main/java/com/example/blog/controller/PostController.java b/src/main/java/com/example/blog/controller/PostController.java index a239a7c..a9d11ca 100644 --- a/src/main/java/com/example/blog/controller/PostController.java +++ b/src/main/java/com/example/blog/controller/PostController.java @@ -6,10 +6,16 @@ import com.example.blog.model.Post; import com.example.blog.repository.PostRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/posts") @@ -25,7 +31,7 @@ public List getAllPosts() { // Create a new Post @PostMapping - public Post createPost(@RequestBody Post post) { + public Post createPost(@Valid @RequestBody Post post) { return postRepository.save(post); } @@ -39,5 +45,14 @@ public ResponseEntity getPostById(@PathVariable(value = "id") Long postId) return ResponseEntity.ok().body(post); } - // Other CRUD methods omitted for brevity. + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidationExceptions(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); + } } diff --git a/src/main/java/com/example/blog/model/Post.java b/src/main/java/com/example/blog/model/Post.java index b2511b0..153b0b1 100644 --- a/src/main/java/com/example/blog/model/Post.java +++ b/src/main/java/com/example/blog/model/Post.java @@ -6,14 +6,16 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import java.util.Objects; +import javax.validation.constraints.NotBlank; @Entity public class Post { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @NotBlank(message = "Title cannot be empty") private String title; + @NotBlank(message = "Body cannot be empty") private String body; private int views; diff --git a/target/classes/com/example/blog/controller/PostController.class b/target/classes/com/example/blog/controller/PostController.class index 573669da7d3a39bd255042bcb4818f8b94aa7d59..17097ed523031577f6fbe0535079086f77728dca 100644 GIT binary patch literal 5360 zcmcgwiFe$@8UKwhC2Kh`1{?x}24Y}+Byz|}Hc4wc4oPb}!8T3@+}ZN;**NZQyg zrA-U;rZ@Dy?;9v}aMIHB?(6H{)Ysq4%DdWFc1-$S-#+bVH1plx_xncr*MF|O1>hL| znL!=u4K$c&M3X@41^c3HxwbcLoteBK^N~Q)QO9$l;{x^B&WX*~gv|yrCYoUibmjdy zOD@=RMORvru0L(%eJ=`p*Oh^F)(@kB%2Z%Wkq<^C|DDJW76p2SR*?slWldh)oomJx zY&Fno;uf@}NnVpwz?gEpLZ9mjv}HSow27t2ajhXIj5OohOx%ukfrik&C0|lF}laZ zJFt`9pOy@Ul_VYVz3CObG~raq%PUfAtW+$zjtp`c+=pETI!xS;2WWoP_7x!l&3-WG zhH`u+@aF~g#uCs9m^h`%uP05TwTJ~wYQXv%3<5t0+aU06 z6Ys$g!NtxiH3jx0(~fRP&uhX-NGnwsM{vf#Srd<9l+aRjqpE3D@l0wvKO?X+$+%J= z6-Elu#_^be2@~h=UV&X{mgzh^#bg|d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