Skip to content

Commit

Permalink
refactor: foreach문 map으로 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
zinzoddari committed Jan 4, 2024
1 parent 4771425 commit bb0e15c
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.zalando.problem.Status;

import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -54,11 +53,11 @@ public ResponseEntity<Problem> methodArgumentNotValidExceptionHandler(MethodArgu

BindingResult bindingResult = e.getBindingResult();

List<InvalidResponse> responses = new ArrayList<>();

bindingResult.getFieldErrors().forEach(fieldError -> {
responses.add(new InvalidResponse(fieldError.getField(), fieldError.getDefaultMessage(), fieldError.getRejectedValue()));
});
List<InvalidResponse> responses = bindingResult.getFieldErrors().stream()
.map(fieldError -> new InvalidResponse(fieldError.getField()
, fieldError.getDefaultMessage()
, fieldError.getRejectedValue())
).toList();

Problem problem = Problem.builder()
.withStatus(Status.BAD_REQUEST)
Expand All @@ -79,11 +78,11 @@ public ResponseEntity<Problem> methodArgumentNotValidExceptionHandler(MethodArgu
public Problem constraintViolationExceptionHandler(ConstraintViolationException e) {
log.error("[ 400 ERROR ] : ", e);

List<InvalidResponse> responses = new ArrayList<>();

e.getConstraintViolations().forEach(fieldError -> {
responses.add(new InvalidResponse(fieldError.getPropertyPath().toString(), fieldError.getMessage(), fieldError.getInvalidValue()));
});
List<InvalidResponse> responses = e.getConstraintViolations().stream()
.map(fieldError -> new InvalidResponse(fieldError.getPropertyPath().toString()
, fieldError.getMessage()
, fieldError.getInvalidValue())
).toList();

return Problem.builder()
.withStatus(Status.BAD_REQUEST)
Expand Down

0 comments on commit bb0e15c

Please sign in to comment.