Um einen Problem-RFC-Handler in eine Quarkus-Applikation zu integrieren, hängt die genaue Vorgehensweise davon ab, was du mit "Problem RFC Handler" meinst. In der Regel geht es jedoch in diesem Kontext um die Einbindung eines Fehlerbehandlungsmechanismus, idealerweise mit RFC 7807-konformen "Problem Details" als Fehlerantworten für HTTP-APIs.
1. RFC 7807 – "Problem Details for HTTP APIs"
RFC 7807 definiert ein Standardformat für maschinenlesbare HTTP-Fehlermeldungen, z. B.:
json
{
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"status": 403,
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/account/12345/msgs/abc"
}
2. Lösung: Quarkus + Problem Spring Web (Zalando)
Du kannst hierfür die Zalando Problem Library oder den Nachfolger Problem Spring Web verwenden. Quarkus basiert jedoch nicht auf Spring, daher ist eine native Einbindung anders.
In Java selbst kannst du eine einfache Lösung mithilfe von ExceptionMapper in JAX-RS bauen — das ist der Quarkus-Weg.
3. Schritt-für-Schritt Integration in Quarkus
🔧 A. ProblemDetails-Klasse (RFC 7807-konform)
java
import javax.json.bind.annotation.JsonbProperty;
public class ProblemDetails {
private String type;
private String title;
private int status;
private String detail;
private String instance;
// Konstruktoren
public ProblemDetails() {
}
public ProblemDetails(String type, String title, int status, String detail, String instance) {
this.type = type;
this.title = title;
this.status = status;
this.detail = detail;
this.instance = instance;
}
// Getter und Setter
// ...
}
🔧 B. Custom Exception
java
public class MyAppException extends RuntimeException {
private final int status;
private final String type;
private final String detail;
public MyAppException(int status, String type, String message, String detail) {
super(message);
this.status = status;
this.type = type;
this.detail = detail;
}
public int getStatus() {
return status;
}
public String getType() {
return type;
}
public String getDetail() {
return detail;
}
}
🔧 C. ExceptionMapper für MyAppException
java
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class MyAppExceptionMapper implements ExceptionMapper<MyAppException> {
@Override
public Response toResponse(MyAppException ex) {
ProblemDetails problem = new ProblemDetails(
ex.getType(),
ex.getMessage(),
ex.getStatus(),
ex.getDetail(),
"" // Optionale URI der Instanz
);
return Response.status(ex.getStatus())
.entity(problem)
.build();
}
}
🔧 D. Globaler Fallback-Handler (Optional)
Falls du alle sonstigen Exceptions auch im RFC 7807-Format behandeln willst:
java
@Provider
public class GlobalExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable ex) {
ProblemDetails problem = new ProblemDetails(
"about:blank",
"Internal Server Error",
500,
ex.getMessage(),
""
);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(problem)
.build();
}
}
4. Test in einem REST-Endpoint
java
@Path("/api/test")
@Produces(MediaType.APPLICATION_JSON)
public class TestResource {
@GET
public String test() {
throw new MyAppException(
403,
"https://example.com/errors/forbidden",
"Access Denied",
"You must be an admin to perform this action."
);
}
}
5. Optional: Problem-JSON automatisch mit Content-Type
Du kannst in dem ExceptionMapper auch den Content-Type explizit auf application/problem+json setzen, was RFC 7807-konform ist:
Aber empfehlenswert ist eher die native Lösung in Quarkus über ExceptionMapper.
✅ Fazit
Ein "Problem RFC Handler" lässt sich in Quarkus sauber mit JAX-RS-Mitteln integrieren – primär über einen @Provider-annotierten ExceptionMapper. Damit kannst du HTTP-Fehler im application/problem+json Format zurückgeben.
Wenn du mir sagen kannst, ob du z. B. Zalando Problem nutzen willst, oder was dein konkreter Use Case ist (z. B. Spring-Migration → Quarkus), kann ich dir eine gezielte Beispielanwendung machen.
Möchtest du ein komplettes GitHub-Beispiel oder einen Quarkus-Code-Snippet zum Einbinden?