- Notifications
You must be signed in to change notification settings - Fork5
Quarkus extension for application/problem+json (RFC7807)
License
cloudstark/quarkus-zalando-problem-extension
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Extension to useZalando Problem in yourQuarkus JAX-RSapplication. The extension will register anExceptionMapper that is responsible to map catched Exceptions intovalidapplication/problem+json to be used as the payload of the HTTP Response.
The Problem extension is not available in Maven Central. For now you have to clone the repository and install theextension in your local maven repository. Then follow these steps to write and deploy a simple JAX-RS application:
Create a new project using the Quarkusarchetype:
mvn io.quarkus:quarkus-maven-plugin:1.2.1.Final:create \ -DprojectGroupId=org.acme.rfc7807 \ -DprojectArtifactId=rfc7807 \ -DclassName="org.acme.rest.json.CalcResource" \ -Dpath="/calc" \ -Dextensions="resteasy-jsonb"
Add the following dependency to yourpom.xml:
<dependency> <groupId>solutions.cloudstark.quarkus</groupId> <artifactId>quarkus-zalando-problem</artifactId> <version>0.0.2-SNAPSHOT</version></dependency>
Create a service definition insrc/main/java/org/acme/rest/json/CalcResource.java
importorg.zalando.problem.Problem;importorg.zalando.problem.Status;importorg.zalando.problem.violations.ConstraintViolationProblem;importorg.zalando.problem.violations.Violation;importjavax.ws.rs.GET;importjavax.ws.rs.Path;importjavax.ws.rs.PathParam;importjavax.ws.rs.Produces;importjavax.ws.rs.core.Context;importjavax.ws.rs.core.UriInfo;importjava.util.Arrays;importstaticjavax.ws.rs.core.MediaType.TEXT_PLAIN;@Path("/calc")publicclassCalcResource {@ContextUriInfouriInfo;@GET@Path("/{a: [0-9]+}:{b: [0-9]+}")@Produces(TEXT_PLAIN)publicintdivide(@PathParam("a")inta,@PathParam("b")intb) {returna /b; }@GET@Path("/exception")@Produces(TEXT_PLAIN)publicvoidexception() {thrownewRuntimeException("This is a runtime exception!"); }@GET@Path("/problem")@Produces(TEXT_PLAIN)publicvoidproblem() {throwProblem.builder() .withStatus(Status.BAD_REQUEST) .withTitle("Strange problem!") .build(); }@GET@Path("/violation")@Produces(TEXT_PLAIN)publicvoidviolation() {thrownewConstraintViolationProblem(Status.BAD_REQUEST,Arrays.asList(newViolation("name","must not be null"))); }}
Note: You will have to adjust the generated test insrc/test/java - otherwise the build will fail.
$ curl localhost:8080/calc/20:0{ "status": 500, "title": "/ by zero", "detail": "java.lang.ArithmeticException: / by zero", "instance": "/calc/20:0"}$ curl localhost:8080/calc/violation{ "type": "https://zalando.github.io/problem/constraint-violation", "status": 400, "title": "Constraint Violation", "violations": [ { "field": "name", "message": "must not be null" } ]}mvn packagejava -jar target/rfc7807-1.0-SNAPSHOT-runner.jar
mvn package -P native./target/rfc7807-1.0-SNAPSHOT-runner
- Jackson support
- more unit tests
About
Quarkus extension for application/problem+json (RFC7807)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.