Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Quarkus extension for application/problem+json (RFC7807)

License

NotificationsYou must be signed in to change notification settings

cloudstark/quarkus-zalando-problem-extension

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.

Getting Started

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:

Setup Project

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>

Define JAX-RS resource

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"    }  ]}

Run

JVM Mode

mvn packagejava -jar target/rfc7807-1.0-SNAPSHOT-runner.jar

Native Mode

mvn package -P native./target/rfc7807-1.0-SNAPSHOT-runner

Todos

  • Jackson support
  • more unit tests

About

Quarkus extension for application/problem+json (RFC7807)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp