### Instantiating Custom Problem
Source: https://github.com/zalando/problem/blob/main/README.md
Provides an example of how to create an instance of the custom `OutOfStockProblem` class.
```java
new OutOfStockProblem("B00027Y5QG");
```
--------------------------------
### Problem JSON Output
Source: https://github.com/zalando/problem/blob/main/README.md
Example JSON output for a problem instance created using the Problem Builder or a custom problem class.
```json
{
"type": "https://example.org/out-of-stock",
"title": "Out of Stock",
"status": 400,
"detail": "Item B00027Y5QG is no longer available"
}
```
--------------------------------
### Problem JSON Output with Custom Property
Source: https://github.com/zalando/problem/blob/main/README.md
Example JSON output for a problem instance that includes a custom property.
```json
{
"type": "https://example.org/out-of-stock",
"title": "Out of Stock",
"status": 400,
"detail": "Item B00027Y5QG is no longer available",
"product": "B00027Y5QG"
}
```
--------------------------------
### Building ThrowableProblem with Causal Chain
Source: https://github.com/zalando/problem/blob/main/README.md
Illustrates how to construct a ThrowableProblem with a nested cause, demonstrating the causal chain pattern. The example uses Problem.builder() to create problems with types, titles, statuses, and causes.
```java
ThrowableProblem problem = Problem.builder()
.withType(URI.create("https://example.org/order-failed"))
.withTitle("Order failed")
.withStatus(BAD_REQUEST)
.withCause(Problem.builder()
.withType(URI.create("https://example.org/out-of-stock"))
.withTitle("Out of Stock")
.withStatus(BAD_REQUEST)
.build())
.build();
problem.getCause(); // standard API of java.lang.Throwable
```
--------------------------------
### Jackson Deserialization
Source: https://github.com/zalando/problem/blob/main/README.md
Example of deserializing a problem using Jackson, including registering subtypes.
```java
Problem problem = mapper.readValue(.., Problem.class);
mapper.registerSubtypes(OutOfStockProblem.class);
```
--------------------------------
### Jackson Polymorphic Deserialization Setup
Source: https://github.com/zalando/problem/blob/main/README.md
Illustrates the necessary annotations (`@JsonTypeName`, `@JsonCreator`) for Jackson to handle custom problem types during deserialization.
```java
@JsonTypeName(OutOfStockProblem.TYPE_VALUE)
public final class OutOfStockProblem implements Problem {
@JsonCreator
public OutOfStockProblem(final String product) {
```
--------------------------------
### Problem Builder Usage
Source: https://github.com/zalando/problem/blob/main/README.md
Demonstrates how to use the Problem Builder to construct a problem instance with standard fields like type, title, status, and detail.
```java
Problem.builder()
.withType(URI.create("https://example.org/out-of-stock"))
.withTitle("Out of Stock")
.withStatus(BAD_REQUEST)
.withDetail("Item B00027Y5QG is no longer available")
.build();
```
--------------------------------
### Handling Exceptional Interface with Propagate
Source: https://github.com/zalando/problem/blob/main/README.md
Shows an alternative approach using the Exceptional interface, which requires calling the propagate() method to handle potential problems.
```java
try {
throw mapper.readValue(.., Exceptional.class).propagate();
} catch (OutOfStockProblem e) {
...
}
```
--------------------------------
### Custom Problem Implementation
Source: https://github.com/zalando/problem/blob/main/README.md
Illustrates the creation of a custom problem class `OutOfStockProblem` by extending `AbstractThrowableProblem`.
```java
@Immutable
public final class OutOfStockProblem extends AbstractThrowableProblem {
static final URI TYPE = URI.create("https://example.org/out-of-stock");
private final String product;
public OutOfStockProblem(final String product) {
super(TYPE, "Out of Stock", BAD_REQUEST, format("Item %s is no longer available", product));
this.product = product;
}
public String getProduct() {
return product;
}
}
```
--------------------------------
### Extending AbstractThrowableProblem
Source: https://github.com/zalando/problem/blob/main/README.md
Demonstrates extending `AbstractThrowableProblem` for creating throwable problems.
```java
public final class OutOfStockProblem extends AbstractThrowableProblem {
// constructor
}
```
--------------------------------
### Catching Specific Problems with ThrowableProblem
Source: https://github.com/zalando/problem/blob/main/README.md
Demonstrates how to catch specific problem types like OutOfStockProblem, InsufficientFundsProblem, and InvalidCouponProblem using instanceof checks within a try-catch block. It also includes a general catch for ThrowableProblem.
```java
try {
throw mapper.readValue(.., ThrowableProblem.class);
} catch (OutOfStockProblem e) {
tellTheCustomerTheProductIsNoLongerAvailable(e.getProduct());
} catch (InsufficientFundsProblem e) {
askCustomerToUseDifferentPaymentMethod(e.getBalance(), e.getDebit());
} catch (InvalidCouponProblem e) {
askCustomerToUseDifferentCoupon(e.getCouponCode());
} catch (ThrowableProblem e) {
tellTheCustomerSomethingWentWrong();
}
```
--------------------------------
### Implementing Exceptional Interface
Source: https://github.com/zalando/problem/blob/main/README.md
Shows how to implement the `Exceptional` marker interface for custom exception classes.
```java
public final class OutOfStockProblem extends BusinessException implements Exceptional
```
--------------------------------
### Creating a Problem with Status and Detail
Source: https://github.com/zalando/problem/blob/main/README.md
Generates a Problem object with an HTTP status code and a custom detail message. This allows for providing additional context to the error.
```java
Problem.valueOf(Status.SERVICE_UNAVAILABLE, "Database not reachable");
```
--------------------------------
### Problem Builder with Custom Property
Source: https://github.com/zalando/problem/blob/main/README.md
Shows how to add custom properties to a problem instance using the Problem Builder's `with()` method.
```java
Problem.builder()
.withType(URI.create("https://example.org/out-of-stock"))
.withTitle("Out of Stock")
.withStatus(BAD_REQUEST)
.withDetail("Item B00027Y5QG is no longer available")
.with("product", "B00027Y5QG")
.build();
```
--------------------------------
### Maven Dependencies for Problem Library
Source: https://github.com/zalando/problem/blob/main/README.md
Includes the core problem library, Jackson integration, and Gson integration. These dependencies are necessary for using the library and its serialization capabilities.
```xml
org.zalando
problem
${problem.version}
org.zalando
jackson-datatype-problem
${problem.version}
org.zalando
problem-gson
${problem.version}
```
--------------------------------
### Enabling Stack Trace Serialization
Source: https://github.com/zalando/problem/blob/main/README.md
Shows how to configure the ObjectMapper with ProblemModule to enable the serialization of stack traces within problems. This is useful for debugging in integration environments.
```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ProblemModule().withStackTraces());
```
--------------------------------
### JSON Representation of ThrowableProblem with Cause
Source: https://github.com/zalando/problem/blob/main/README.md
Provides the JSON output for a ThrowableProblem that includes a nested cause, showing how the causal chain is represented in the serialized format.
```json
{
"type": "https://example.org/order-failed",
"title": "Order failed",
"status": 400,
"cause": {
"type": "https://example.org/out-of-stock",
"title": "Out of Stock",
"status": 400,
"detail": "Item B00027Y5QG is no longer available"
}
}
```
--------------------------------
### Problem JSON Representation (Status and Detail)
Source: https://github.com/zalando/problem/blob/main/README.md
The JSON output for a Problem created with an HTTP status code and a detail message. It includes 'title', 'status', and 'detail' fields.
```json
{
"title": "Service Unavailable",
"status": 503,
"detail": "Database not reachable"
}
```
--------------------------------
### Creating a Problem with HTTP Status
Source: https://github.com/zalando/problem/blob/main/README.md
Generates a Problem object using only an HTTP status code. This creates a minimal problem representation with a title corresponding to the status phrase.
```java
Problem.valueOf(Status.NOT_FOUND);
```
--------------------------------
### StackTraceProcessor Interface
Source: https://github.com/zalando/problem/blob/main/README.md
Defines the StackTraceProcessor interface, which allows for custom processing of stack traces during problem creation. This can be used to filter or modify stack trace elements.
```java
public interface StackTraceProcessor {
Collection process(final Collection elements);
}
```
--------------------------------
### JSON Representation with Stack Trace
Source: https://github.com/zalando/problem/blob/main/README.md
Displays the JSON output of a problem that includes a 'stacktrace' property, which is added when stack trace serialization is enabled.
```json
{
"type": "about:blank",
"title": "Unprocessable Entity",
"status": 400,
"stacktrace": [
"org.example.Example.execute(Example.java:17)",
"org.example.Example.main(Example.java:11)"
]
}
```
--------------------------------
### Registering ProblemModule with ObjectMapper
Source: https://github.com/zalando/problem/blob/main/README.md
Configures Jackson's ObjectMapper to recognize and serialize Problem objects by registering the ProblemModule. This allows for seamless JSON serialization of API problems.
```java
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ProblemModule());
```
--------------------------------
### Java Module Declaration
Source: https://github.com/zalando/problem/blob/main/README.md
Declares the necessary modules for using the Problem library in a Java 9+ modular project. It specifies the core module and optional Jackson or Gson modules.
```java
module org.example {
requires org.zalando.problem;
// pick needed dependencies
requires org.zalando.problem.jackson;
requires org.zalando.problem.gson;
}
```
--------------------------------
### Auto-registering Problem Modules with ObjectMapper
Source: https://github.com/zalando/problem/blob/main/README.md
Automatically registers all available Problem modules with Jackson's ObjectMapper using SPI. This is a convenient way to enable problem serialization without explicit module registration.
```java
ObjectMapper mapper = new ObjectMapper()
.findAndRegisterModules();
```
--------------------------------
### Problem JSON Representation (Status Only)
Source: https://github.com/zalando/problem/blob/main/README.md
The JSON output for a Problem created with only an HTTP status code. It includes the 'title' and 'status' fields as per RFC 7807.
```json
{
"title": "Not Found",
"status": 404
}
```
--------------------------------
### Problem Details Object
Source: https://github.com/zalando/problem/wiki/Constraint-Violation
The core structure of a Problem Details object, as defined by RFC 7807. It includes fields like 'type', 'title', 'status', 'detail', and 'instance'.
```JSON
{
"type": "urn:problem:my-application:invalid-input",
"title": "Invalid input data",
"status": 400,
"detail": "The provided data does not meet the required format.",
"instance": "/problems/invalid-input/12345"
}
```
--------------------------------
### Constraint Violation Problem
Source: https://github.com/zalando/problem/wiki/Constraint-Violation
A specific type of Problem Details object used to represent constraint violations, often encountered during data validation. It includes a 'violations' array detailing each specific constraint that failed.
```JSON
{
"type": "urn:problem:my-application:constraint-violation",
"title": "Constraint Violation",
"status": 422,
"detail": "One or more constraints were violated.",
"violations": [
{
"path": "email",
"message": "Must be a valid email address."
},
{
"path": "age",
"message": "Must be a positive integer."
}
]
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.