From c69cc6a36a825a4cdc771dc2ede2620c861659a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Witkowski?= Date: Mon, 30 Oct 2023 20:24:55 +0100 Subject: [PATCH] Simplify setup for custom ProblemPostProcessor --- README.md | 4 ---- .../resteasy/problem/deployment/ProblemProcessor.java | 7 +++++++ .../quarkus/resteasy/problem/CustomPostProcessor.java | 7 ------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 27d4fda..cd6cca6 100644 --- a/README.md +++ b/README.md @@ -239,13 +239,9 @@ quarkus.log.category.http-problem.level=OFF # disables all problems-related logg If you want to intercept, change or augment a mapped `HttpProblem` before it gets serialized into raw HTTP response body, you can create a bean extending `ProblemPostProcessor`, and override `apply` method. -**Important: Make sure your implementation is thread-safe, as it may potentially be called concurrently if multiple -requests throw exceptions at the same time.** - Example: ```java @ApplicationScoped -@Startup // makes sure bean is instantiated eagerly on startup class CustomPostProcessor implements ProblemPostProcessor { @Inject // acts like normal bean, DI works fine etc diff --git a/deployment/src/main/java/com/tietoevry/quarkus/resteasy/problem/deployment/ProblemProcessor.java b/deployment/src/main/java/com/tietoevry/quarkus/resteasy/problem/deployment/ProblemProcessor.java index 8ca4954..1b6bb88 100644 --- a/deployment/src/main/java/com/tietoevry/quarkus/resteasy/problem/deployment/ProblemProcessor.java +++ b/deployment/src/main/java/com/tietoevry/quarkus/resteasy/problem/deployment/ProblemProcessor.java @@ -5,8 +5,10 @@ import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT; import com.tietoevry.quarkus.resteasy.problem.ProblemRuntimeConfig; +import com.tietoevry.quarkus.resteasy.problem.postprocessing.ProblemPostProcessor; import com.tietoevry.quarkus.resteasy.problem.postprocessing.ProblemRecorder; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.arc.deployment.UnremovableBeanBuildItem; import io.quarkus.deployment.Capabilities; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -169,6 +171,11 @@ void registerCustomPostProcessors(ProblemRecorder recorder) { recorder.registerCustomPostProcessors(); } + @BuildStep + UnremovableBeanBuildItem markPostProcessorsUnremovable() { + return UnremovableBeanBuildItem.beanTypes(ProblemPostProcessor.class); + } + @Record(RUNTIME_INIT) @BuildStep void applyRuntimeConfig(ProblemRecorder recorder, ProblemRuntimeConfig config) { diff --git a/integration-test/src/main/java/com/tietoevry/quarkus/resteasy/problem/CustomPostProcessor.java b/integration-test/src/main/java/com/tietoevry/quarkus/resteasy/problem/CustomPostProcessor.java index b42ac96..95033c2 100644 --- a/integration-test/src/main/java/com/tietoevry/quarkus/resteasy/problem/CustomPostProcessor.java +++ b/integration-test/src/main/java/com/tietoevry/quarkus/resteasy/problem/CustomPostProcessor.java @@ -2,18 +2,11 @@ import com.tietoevry.quarkus.resteasy.problem.postprocessing.ProblemContext; import com.tietoevry.quarkus.resteasy.problem.postprocessing.ProblemPostProcessor; -import io.quarkus.runtime.Startup; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.inject.Inject; -import jakarta.validation.Validator; @ApplicationScoped -@Startup class CustomPostProcessor implements ProblemPostProcessor { - @Inject - Validator validator; - @Override public HttpProblem apply(HttpProblem problem, ProblemContext context) { return HttpProblem.builder(problem)