Skip to content

Commit

Permalink
Adding validation (fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexertech committed Oct 25, 2023
1 parent ba44d4d commit 268b798
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/example/blog/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,7 +31,7 @@ public List<Post> getAllPosts() {

// Create a new Post
@PostMapping
public Post createPost(@RequestBody Post post) {
public Post createPost(@Valid @RequestBody Post post) {
return postRepository.save(post);
}

Expand All @@ -39,5 +45,14 @@ public ResponseEntity<Post> getPostById(@PathVariable(value = "id") Long postId)
return ResponseEntity.ok().body(post);
}

// Other CRUD methods omitted for brevity.
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> 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);
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/example/blog/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Binary file modified target/classes/com/example/blog/controller/PostController.class
Binary file not shown.
Binary file modified target/classes/com/example/blog/model/Post.class
Binary file not shown.

0 comments on commit 268b798

Please sign in to comment.