-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added data validation and global exception handler #4
Conversation
private String author; | ||
@NotNull | ||
private String isbn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use ISBN annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GJ. Let's improve it a bit
pom.xml
Outdated
@@ -71,6 +71,12 @@ | |||
<artifactId>liquibase-maven-plugin</artifactId> | |||
<version>4.23.1</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.hibernate.validator</groupId> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Spring Boot it's better to choose starters over concrete dependencies, so I suggest to use validation starter
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CreateBookRequestDto { | ||
@NotNull | ||
private String title; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the size of title to not allow empty titles and to not exceed the maximum size set by DB schema, the same for author field
private String author; | ||
@NotNull | ||
private String isbn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isbn has other validation rule except of not null (see Oleg's suggestion)
) { | ||
Map<String, Object> body = new LinkedHashMap<>(); | ||
body.put("timestamp", LocalDateTime.now()); | ||
body.put("status", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually invalid arguments it's 422 response code. Also, define the common response format for the application and create a class with exception response body.
…ded http response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
No description provided.