Skip to content

Реализованны Модели данных и REST-контроллеры#2

Merged
Max-Browckin merged 2 commits intomainfrom
controllers-films-users
Apr 22, 2025
Merged

Реализованны Модели данных и REST-контроллеры#2
Max-Browckin merged 2 commits intomainfrom
controllers-films-users

Conversation

@Max-Browckin
Copy link
Owner

No description provided.

Copy link

@avfyodorov avfyodorov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добрый день, Максим! 

Очень хорошая работа, от меня всего пара небольших уточнений.

import java.time.LocalDate;

@Data
public class Film {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необязательно, на Ваше усмотрение. Для этих полей можно было бы добавить ограничительные аннотации, например:

public class Film
...
	@NotBlank
	private String name;
	@Size(min = 1, max = 200)
	private String description;
	@NotNull
	private LocalDate releaseDate;
	@Min(1)
	private long duration; // minutes

Тогда можно будет избавиться от части ручных проверок.

}

@PostMapping
public Film addFilm(@RequestBody Film film) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно добавить аннотацию @Valid, чтобы возложить на Spring проверку входного объекта. 
public Film addFilm(@Valid @RequestBody Film film) {
На усмотрение.

Валидация данных в Spring Boot
https://struchkov.dev/blog/ru/spring-boot-validation/
.---------------------------

Проверка данных — Java & Spring Validation
https://habr.com/ru/post/424819/

import java.time.LocalDate;

@Data
public class User {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необязательно, на Ваше усмотрение.
Здесь такая же ситуация, что и c классом фильм - Можно добавить ограничительные аннотации на поля  класса. Тогда часть проверок будет выполняться средствами Spring в  методе контроллера.

public class User {  
    @NotBlank  
    @Email   
    private String email;  
    @NotBlank  
    private String login;
    ............

@Data
public class User {
private Long id;
private String email;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обратите внимание, что для автоматической проверки нужны не только аннотации над полями класса, но и добавить аннотацию @Valid в соответствующих методах контроллера User
public User create(@Valid @RequestBody final User user) {

Copy link

@avfyodorov avfyodorov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добрый день, Максим!

Замечаний нет.
Работа принята.

@Max-Browckin Max-Browckin merged commit a30097c into main Apr 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants