Skip to content

Commit

Permalink
Simplify setup for custom ProblemPostProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
lwitkowski committed Oct 30, 2023
1 parent 129efd2 commit c69cc6a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c69cc6a

Please sign in to comment.