项目作者: TietoEVRY

项目描述 :
Problem Details for HTTP APIs (RFC-7807) implementation for Quarkus.
高级语言: Java
项目地址: git://github.com/TietoEVRY/quarkus-resteasy-problem.git
创建时间: 2021-03-02T08:25:23Z
项目社区:https://github.com/TietoEVRY/quarkus-resteasy-problem

开源协议:Apache License 2.0

下载


Problem Details for HTTP APIs (RFC-7807) implementation for Quarkus / RESTeasy.

License

Build status
Build status
Build status

RFC7807 Problem extension for Quarkus RESTeasy/JaxRS applications. It maps Exceptions to application/problem+json HTTP responses. Inspired by Zalando Problem library, originally open sourced by Tietoevry, now part of Quarkiverse.

This extension supports:

  • Quarkus 1, 2 and 3
  • quarkus-resteasy-jackson and quarkus-resteasy-jsonb
  • quarkus-rest-jackson and quarkus-rest-jsonb
  • JVM and native mode

Why you should use this extension?

  • consistency - it unifies your REST API error messages, and gives it much needed consistency, no matter which JSON provider (Jackson vs JsonB) or paradigm (classic/blocking vs reactive) you’re using.

  • predictability - no matter what kind of exception is thrown: expected (thrown by you on purpose), or unexpected (not thrown ‘by design’) - your API consumer gets similar, repeatable experience.

  • safety - it helps prevent leakage of some implementation details like stack-traces, DTO/resource class names etc.

  • time-saving - in most cases you will not have to implement your own JaxRS ExceptionMappers anymore, which makes your app smaller, and less error-prone.

See Built-in Exception Mappers Wiki for more details.

From RFC7807:

  1. HTTP [RFC7230] status codes are sometimes not sufficient to convey
  2. enough information about an error to be helpful. While humans behind
  3. Web browsers can be informed about the nature of the problem with an
  4. HTML [W3C.REC-html5-20141028] response body, non-human consumers of
  5. so-called "HTTP APIs" are usually not.

Usage

Quarkus 3.14+

Add this to your pom.xml:

  1. <dependency>
  2. <groupId>io.quarkiverse.resteasy-problem</groupId>
  3. <artifactId>quarkus-resteasy-problem</artifactId>
  4. <version>3.19.0</version>
  5. </dependency>

Quarkus up to 3.13 / Java 17+

### Quarkus 3.X
Quarkus | Java | quarkus-resteasy-problem
——————————-|———|————————————-
< 3.7.0 | 11+ | 3.1.0
>= 3.7.0 && < 3.9.0 | 17+ | 3.7.0
>= 3.9.0 | 17+ | 3.9.0

Make sure proper version of JDK (look for the table above), then run:
shell mvn io.quarkus:quarkus-maven-plugin:${quarkus.version}:create \ -DprojectGroupId=problem \ -DprojectArtifactId=quarkus-resteasy-problem-playground \ -DclassName="problem.HelloResource" \ -Dpath="/hello" \ -Dextensions="resteasy,resteasy-jackson" cd quarkus-resteasy-problem-playground ./mvnw quarkus:add-extension -Dextensions="com.tietoevry.quarkus:quarkus-resteasy-problem:3.9.0"
Or add the following dependency to pom.xml in existing project:
xml <dependency> <groupId>com.tietoevry.quarkus</groupId> <artifactId>quarkus-resteasy-problem</artifactId> <version>3.9.0</version> </dependency>

Quarkus 2.X / Java 11+

Make sure JDK 11 is in your PATH, then run:
shell mvn io.quarkus:quarkus-maven-plugin:2.16.10.Final:create \ -DprojectGroupId=problem \ -DprojectArtifactId=quarkus-resteasy-problem-playground \ -DclassName="problem.HelloResource" \ -Dpath="/hello" \ -Dextensions="resteasy,resteasy-jackson" cd quarkus-resteasy-problem-playground ./mvnw quarkus:add-extension -Dextensions="com.tietoevry.quarkus:quarkus-resteasy-problem:2.2.0
Or add the following dependency to pom.xml in existing project:
xml <dependency> <groupId>com.tietoevry.quarkus</groupId> <artifactId>quarkus-resteasy-problem</artifactId> <version>2.2.0</version> </dependency>

Quarkus 1.X / Java 1.8+

Create a new Quarkus project with the following command:
shell mvn io.quarkus:quarkus-maven-plugin:1.13.7.Final:create \ -DprojectGroupId=problem \ -DprojectArtifactId=quarkus-resteasy-problem-playground \ -DclassName="problem.HelloResource" \ -Dpath="/hello" \ -Dextensions="resteasy,resteasy-jackson,com.tietoevry.quarkus:quarkus-resteasy-problem:1.0.0" cd quarkus-resteasy-problem-playground
Or add the following dependency to pom.xml in existing project:
xml <dependency> <groupId>com.tietoevry.quarkus</groupId> <artifactId>quarkus-resteasy-problem</artifactId> <version>1.0.0</version> </dependency>

Hint: you can also use resteasy-jsonb or reactive equivalents: rest-jackson / rest-jsonb instead of resteasy-jackson

Once you run Quarkus: ./mvnw compile quarkus:dev, and you will find resteasy-problem in the logs:

  1. Installed features: [cdi, resteasy, resteasy-jackson, resteasy-problem]

Now you can throw HttpProblems (using builder or a subclass), JaxRS exceptions (e.g NotFoundException) or ThrowableProblems from Zalando library:

  1. package problem;
  2. import io.quarkiverse.resteasy.problem.HttpProblem;
  3. import jakarta.ws.rs.GET;
  4. import jakarta.ws.rs.Path;
  5. import jakarta.ws.rs.core.Response;
  6. @Path("/hello")
  7. public class HelloResource {
  8. @GET
  9. public String hello() {
  10. throw new HelloProblem("rfc7807-by-example");
  11. }
  12. static class HelloProblem extends HttpProblem {
  13. HelloProblem(String message) {
  14. super(builder()
  15. .withTitle("Bad hello request")
  16. .withStatus(Response.Status.BAD_REQUEST)
  17. .withDetail(message)
  18. .withHeader("X-RFC7807-Message", message)
  19. .with("hello", "world"));
  20. }
  21. }
  22. }

Open http://localhost:8080/hello in your browser, and you should see this response:

  1. HTTP/1.1 400 Bad Request
  2. X-RFC7807-Message: rfc7807-by-example
  3. Content-Type: application/problem+json
  4. {
  5. "status": 400,
  6. "title": "Bad hello request",
  7. "detail": "rfc7807-by-example",
  8. "instance": "/hello",
  9. "hello": "world"
  10. }

This extension will also produce the following log message:

  1. 10:53:48 INFO [http-problem] (executor-thread-1) status=400, title="Bad hello request", detail="rfc7807-by-example"

Exceptions transformed into http 500s (aka server errors) will be logged as ERROR, including full stacktrace.

You may also want to check this article on RFC7807 practical usage.
More on throwing problems: zalando/problem usage

Configuration options

  • (Build time) Include MDC properties in the API response. You have to provide those properties to MDC using MDC.put

    1. quarkus.resteasy.problem.include-mdc-properties=uuid,application,version

    Result:

    1. {
    2. "status": 500,
    3. "title": "Internal Server Error",
    4. "uuid": "d79f8cfa-ef5b-4501-a2c4-8f537c08ec0c",
    5. "application": "awesome-microservice",
    6. "version": "1.0"
    7. }
  • (Runtime) Changes default 400 Bad request response status when ConstraintViolationException is thrown (e.g. by Hibernate Validator)

    1. quarkus.resteasy.problem.constraint-violation.status=422
    2. quarkus.resteasy.problem.constraint-violation.title=Constraint violation

    Result:
    ```json
    HTTP/1.1 422 Unprocessable Entity
    Content-Type: application/problem+json

{
“status”: 422,
“title”: “Constraint violation”,
(…)
}

  1. - (Build time) Enable Smallrye (Microprofile) metrics for http error counters. Requires `quarkus-smallrye-metrics` in the classpath.
  2. Please note that if you use `quarkus-micrometer-registry-prometheus` you don't need this feature - http error metrics will be produced regardless of this setting or presence of this extension.

quarkus.resteasy.problem.metrics.enabled=true

  1. Result:

GET /metrics
application_http_error_total{status=”401”} 3.0
application_http_error_total{status=”500”} 5.0

  1. - (Runtime) Tuning logging

quarkus.log.category.http-problem.level=INFO # default: all problems are logged
quarkus.log.category.http-problem.level=ERROR # only HTTP 5XX problems are logged
quarkus.log.category.http-problem.level=OFF # disables all problems-related logging

  1. ## Custom ProblemPostProcessor
  2. If you want to intercept, change or augment a mapped `HttpProblem` before it gets serialized into raw HTTP response
  3. body, you can create a bean extending `ProblemPostProcessor`, and override `apply` method.
  4. Example:
  5. ```java
  6. @ApplicationScoped
  7. class CustomPostProcessor implements ProblemPostProcessor {
  8. @Inject // acts like normal bean, DI works fine etc
  9. Validator validator;
  10. @Override
  11. public HttpProblem apply(HttpProblem problem, ProblemContext context) {
  12. return HttpProblem.builder(problem)
  13. .with("injected_from_custom_post_processor", "hello world " + context.path)
  14. .build();
  15. }
  16. }

Troubles?

If you have questions, concerns, bug reports, etc, please file an issue in this repository’s Issue Tracker. You may also want to have a look at troubleshooting FAQ.

Contributing

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.
For more details check the contribution guidelines.