public final class JsonNodeSphereRequest extends Base implements SphereRequest<com.fasterxml.jackson.databind.JsonNode>
SphereRequest
which returns JsonNode
instances instead of deserialized Java domain model classes.
On errors like 409, 502 etc. the normal typed exceptions will be thrown.
withCategory(client(), category -> {
final ObjectMapper objectMapper = SphereJsonUtils.newObjectMapper();
final ObjectNode body = objectMapper.createObjectNode();
body.put("version", category.getVersion());
final ArrayNode actions = body.putArray("actions");
final ObjectNode changeNameUpdateAction = objectMapper.createObjectNode();
final String newName = "New Name";
changeNameUpdateAction.put("action", "changeName").set("name", objectMapper.createObjectNode().put("en", newName));
actions.add(changeNameUpdateAction);
final JsonNode jsonNode = client().executeBlocking(JsonNodeSphereRequest.of(POST, "/categories/" + category.getId(), body));
assertThat(jsonNode.get("name").get("en").asText()).isEqualTo(newName);
});
See the test code.
withProduct(client(), product -> {
final String slug = product.getMasterData().getStaged().getSlug().get(Locale.ENGLISH);
final ProductProjectionQuery normalSphereRequest = ProductProjectionQuery.ofStaged()
.withPredicates(m -> m.slug().locale(Locale.ENGLISH).is(slug))
.plusExpansionPaths(m -> m.productType());
final JsonNodeSphereRequest jsonNodeSphereRequest = JsonNodeSphereRequest.of(normalSphereRequest);
assertThat(normalSphereRequest.httpRequestIntent())
.as("a JsonNodeSphereRequest creates the same request to the platform, but differs in the response")
.isEqualTo(jsonNodeSphereRequest.httpRequestIntent());
final PagedQueryResult<ProductProjection> productProjectionPagedSearchResult =
client().executeBlocking(normalSphereRequest);
final JsonNode jsonNode = client().executeBlocking(jsonNodeSphereRequest);//all will be returned as JSON
assertThat(jsonNode.get("results").get(0).get("productType").get("obj").get("description").asText())
.as("the expansion paths are honored")
.isEqualTo("a 'T' shaped cloth");
});
See the test code.
Modifier | Constructor and Description |
---|---|
protected |
JsonNodeSphereRequest(HttpRequestIntent httpRequestIntent) |
Modifier and Type | Method and Description |
---|---|
com.fasterxml.jackson.databind.JsonNode |
deserialize(HttpResponse httpResponse)
Takes an http response and maps it into a Java object of type T.
|
HttpRequestIntent |
httpRequestIntent()
Provides an http request intent, this does not include the execution of it.
|
static JsonNodeSphereRequest |
of(HttpMethod httpMethod,
String path) |
static JsonNodeSphereRequest |
of(HttpMethod httpMethod,
String path,
com.fasterxml.jackson.databind.JsonNode body) |
static JsonNodeSphereRequest |
of(HttpRequestIntent httpRequestIntent) |
static <T> JsonNodeSphereRequest |
of(SphereRequest<T> other) |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
canDeserialize
protected JsonNodeSphereRequest(HttpRequestIntent httpRequestIntent)
@Nullable public com.fasterxml.jackson.databind.JsonNode deserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.canDeserialize(HttpResponse)
if the response can be consumed.deserialize
in interface SphereRequest<com.fasterxml.jackson.databind.JsonNode>
httpResponse
- the http response of the APIpublic HttpRequestIntent httpRequestIntent()
SphereRequest
httpRequestIntent
in interface SphereRequest<com.fasterxml.jackson.databind.JsonNode>
public static JsonNodeSphereRequest of(HttpRequestIntent httpRequestIntent)
public static JsonNodeSphereRequest of(HttpMethod httpMethod, String path)
public static <T> JsonNodeSphereRequest of(SphereRequest<T> other)
public static JsonNodeSphereRequest of(HttpMethod httpMethod, String path, @Nullable com.fasterxml.jackson.databind.JsonNode body)