-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : enable and disable kafka listener (#600)
* feat : enable and disable kafka listener * revert to old test case * feat : implement code review comments
- Loading branch information
1 parent
f42d055
commit a024b8f
Showing
11 changed files
with
261 additions
and
34 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...sample/src/main/java/com/example/springbootkafkasample/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.springbootkafkasample.config; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@ControllerAdvice | ||
class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
ProblemDetail onException(MethodArgumentNotValidException methodArgumentNotValidException) { | ||
ProblemDetail problemDetail = | ||
ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), "Invalid request content."); | ||
problemDetail.setTitle("Constraint Violation"); | ||
List<ApiValidationError> validationErrorsList = methodArgumentNotValidException.getAllErrors().stream() | ||
.map(objectError -> { | ||
FieldError fieldError = (FieldError) objectError; | ||
return new ApiValidationError( | ||
fieldError.getObjectName(), | ||
fieldError.getField(), | ||
fieldError.getRejectedValue(), | ||
Objects.requireNonNull(fieldError.getDefaultMessage(), "")); | ||
}) | ||
.sorted(Comparator.comparing(ApiValidationError::field)) | ||
.toList(); | ||
problemDetail.setProperty("violations", validationErrorsList); | ||
return problemDetail; | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
ProblemDetail handleIllegalArgumentException(IllegalArgumentException ex) { | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), ex.getMessage()); | ||
problemDetail.setTitle("Bad Request"); | ||
return problemDetail; | ||
} | ||
|
||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
public ProblemDetail handleInvalidEnum(HttpMessageNotReadableException ex) { | ||
if (ex.getMessage() | ||
.contains("Cannot deserialize value of type `com.example.springbootkafkasample.dto.Operation`")) { | ||
return ProblemDetail.forStatusAndDetail( | ||
HttpStatusCode.valueOf(400), "Invalid operation value. Allowed values are: START, STOP."); | ||
} | ||
return ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), "Invalid request."); | ||
} | ||
|
||
record ApiValidationError(String object, String field, Object rejectedValue, String message) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...afka-sample/src/main/java/com/example/springbootkafkasample/dto/KafkaListenerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.springbootkafkasample.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record KafkaListenerRequest( | ||
@NotBlank(message = "Container ID must not be blank") String containerId, | ||
@NotNull(message = "Operation must not be null") Operation operation) {} |
6 changes: 6 additions & 0 deletions
6
...boot/boot-kafka-sample/src/main/java/com/example/springbootkafkasample/dto/Operation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.springbootkafkasample.dto; | ||
|
||
public enum Operation { | ||
START, | ||
STOP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.