T
- The type of the value of the custom object.public abstract class CustomObjectCustomJsonMappingByKeyGet<T> extends Base implements Get<CustomObject<T>>
SphereRequest
to fetch one CustomObject
by container and key but using a custom JSON mapper instead of the SDK default one.
Example for an implementation with Google GSON (multiple code snippets):
The class of the custom object value:
import io.sphere.sdk.models.Base;
public class GsonFoo extends Base { private final Long baz; private final String bar; public GsonFoo(final String bar, final Long baz) { this.bar = bar; this.baz = baz; } public String getBar() { return bar; } public long getBaz() { return baz; } }
See the test code.
The class including the wrapper custom object:
import io.sphere.sdk.customobjects.CustomObject;
import java.time.ZonedDateTime;
public class GsonFooCustomObject implements CustomObject<GsonFoo> { private final String container; private final String key; private final GsonFoo value; private final String id; private final Long version; public GsonFooCustomObject(final String container, final String key, final GsonFoo value, final String id, final Long version) { this.container = container; this.key = key; this.value = value; this.id = id; this.version = version; } @Override public String getContainer() { return container; } @Override public ZonedDateTime getCreatedAt() { throw new UnsupportedOperationException("TODO"); } @Override public String getId() { return id; } @Override public String getKey() { return key; } @Override public ZonedDateTime getLastModifiedAt() { throw new UnsupportedOperationException("TODO"); } @Override public GsonFoo getValue() { return value; } @Override public Long getVersion() { return version; } }
See the test code.
The implementation of the getter:
import com.google.gson.Gson;
import io.sphere.sdk.client.SphereRequestUtils;
import io.sphere.sdk.customobjects.CustomObject;
import io.sphere.sdk.customobjects.queries.CustomObjectCustomJsonMappingByKeyGet;
import io.sphere.sdk.http.HttpResponse;
public class GsonFooCustomObjectByKeyGet extends CustomObjectCustomJsonMappingByKeyGet<GsonFoo> { public GsonFooCustomObjectByKeyGet(final String container, final String key) { super(container, key); } @Override protected CustomObject<GsonFoo> deserializeCustomObject(final HttpResponse httpResponse) { final String jsonAsString = SphereRequestUtils.getBodyAsString(httpResponse); return new Gson().fromJson(jsonAsString, GsonFooCustomObject.class); } }
See the test code.
An execution example:
CustomObjectFixtures.withCustomObject(client(), co -> { final String container = co.getContainer(); final String key = co.getKey(); final Get<CustomObject<GsonFoo>> fetch = new GsonFooCustomObjectByKeyGet(container, key); final CustomObject<GsonFoo> customObject = client().executeBlocking(fetch); assertThat(customObject).isNotNull(); assertThat(customObject.toReference()).isEqualTo(co.toReference()); final GsonFoo value = customObject.getValue(); assertThat(value).isEqualTo(new GsonFoo("aString", 5L)); });
See the test code.
CustomObject
Constructor and Description |
---|
CustomObjectCustomJsonMappingByKeyGet(String container,
String key) |
Modifier and Type | Method and Description |
---|---|
boolean |
canDeserialize(HttpResponse httpResponse)
Checks if the response can be handled by
SphereRequest.deserialize(HttpResponse) . |
CustomObject<T> |
deserialize(HttpResponse httpResponse)
Takes an http response and maps it into a Java object of type T.
|
protected abstract CustomObject<T> |
deserializeCustomObject(HttpResponse httpResponse) |
HttpRequestIntent |
httpRequestIntent()
Provides an http request intent, this does not include the execution of it.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
deserialize
canDeserialize, httpRequestIntent
public HttpRequestIntent httpRequestIntent()
SphereRequest
httpRequestIntent
in interface SphereRequest<CustomObject<T>>
@Nullable public final CustomObject<T> deserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.canDeserialize(HttpResponse)
if the response can be consumed.deserialize
in interface SphereRequest<CustomObject<T>>
deserialize
in interface Get<CustomObject<T>>
httpResponse
- the http response of the APIprotected abstract CustomObject<T> deserializeCustomObject(HttpResponse httpResponse)
public boolean canDeserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.deserialize(HttpResponse)
.
Use case 1: A http response returns 404 and the this SphereRequest
can handle this error by returning an empty optional, an empty list or throwing a domain specific exception.canDeserialize
in interface SphereRequest<CustomObject<T>>
httpResponse
- the http response which shall be transformed