77.2. Validation (#9)

This commit is contained in:
Art
2021-09-09 21:24:15 +03:00
parent 6c4b06233d
commit beaab1d298
5 changed files with 217 additions and 38 deletions

View File

@ -15,6 +15,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import static org.springframework.http.HttpStatus.OK;
@Slf4j
@ -56,7 +58,7 @@ public class UserResource {
}
@PostMapping("add")
public User addNewUser(UserDto userDto) {
public User addNewUser(@Valid UserDto userDto) {
log.debug("User DTO: {}", userDto);
return userService.addNewUser(userDto);
}

View File

@ -7,15 +7,26 @@ import lombok.NoArgsConstructor;
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserDto {
@NotEmpty(message = "Should not be empty")
private String firstName;
@NotEmpty(message = "Should not be empty")
private String lastName;
@NotEmpty(message = "Should not be empty")
private String username;
@NotEmpty(message = "Should not be empty")
@Email(message = "Must match email format")
private String email;
@NotNull(message = "Role is mandatory")
private Role role;
private boolean isNonLocked;
private boolean isActive;

View File

@ -13,6 +13,7 @@ import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.persistence.NoResultException;
import java.io.IOException;
import java.util.Objects;
import java.util.stream.Collectors;
import static net.shyshkin.study.fullstack.supportportal.backend.utility.HttpResponseUtility.createHttpResponse;
import static org.springframework.http.HttpStatus.*;
@ -81,10 +83,19 @@ public class ExceptionHandling {
@ExceptionHandler(Exception.class)
public ResponseEntity<HttpResponse> internalServerErrorException(Exception exception) {
log.error(exception.getMessage());
log.error(exception.getMessage(), exception);
return createHttpResponse(INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MSG);
}
@ExceptionHandler({BindException.class})
public ResponseEntity<HttpResponse> validationExceptionHandler(BindException exception) {
String fieldsWithErrors = exception.getFieldErrors().stream()
.map(fe -> fe.getField() + ":" + fe.getDefaultMessage())
.collect(Collectors.joining(","));
String message = "Error(s) in parameters: [" + fieldsWithErrors + "]";
return createHttpResponse(BAD_REQUEST, message);
}
@ExceptionHandler(NoResultException.class)
public ResponseEntity<HttpResponse> notFoundException(NoResultException exception) {
log.error(exception.getMessage());