Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ public final class OutOfStockProblem implements Problem {
Jackson is now able to deserialize specific problems into their respective types. By default, e.g. if a type is not
associated with a class, it will fall back to a `DefaultProblem`.

When deserializing directly into a concrete custom subtype of
`AbstractThrowableProblem`, disable polymorphic type resolution on that subtype:

```java
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
public final class OutOfStockProblem extends AbstractThrowableProblem {
// ...
}
```

### Catching problems

If you read about [Throwing problems](#throwing-problems) already, you should be familiar with `ThrowableProblem`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ final class ProblemMixInTest {
mapper.registerSubtypes(OutOfStockException.class);
}

@Test
void shouldDeserializeConcreteThrowableProblemSubtype() throws Exception {
final ResourceNotFoundProblem original =
new ResourceNotFoundProblem("123", "Resource not found");

final String json = mapper.writeValueAsString(original);

final ResourceNotFoundProblem deserialized =
mapper.readValue(json, ResourceNotFoundProblem.class);

assertThat(deserialized.getType(), equalTo(original.getType()));
assertThat(deserialized.getTitle(), equalTo(original.getTitle()));
assertThat(deserialized.getStatus(), equalTo(original.getStatus()));
assertThat(deserialized.getDetail(), equalTo(original.getDetail()));
assertThat(deserialized.getId(), equalTo(original.getId()));
}

@Test
void shouldSerializeDefaultProblem() throws JsonProcessingException {
final Problem problem = Problem.valueOf(Status.NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.zalando.problem.jackson;

import java.net.URI;

import org.zalando.problem.AbstractThrowableProblem;
import org.zalando.problem.Status;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
public final class ResourceNotFoundProblem extends AbstractThrowableProblem {

private static final URI TYPE = URI.create(
"https://your-company.com/problems/ResourceNotFoundProblem.html"
);

private final String id;

@JsonCreator
public ResourceNotFoundProblem(
@JsonProperty("id") final String id,
@JsonProperty("detail") final String detail
) {
super(TYPE, "Resource not found", Status.NOT_FOUND, detail);
this.id = id;
}

public String getId() {
return id;
}
}