T
- The type of the value of this custom object.public abstract class CustomObjectCustomJsonMappingUpsertCommand<T> extends Base implements CreateCommand<CustomObject<T>>
Example using Google GSON (multiple code snippets):
The execution of the command:
final GsonFoo value = new GsonFoo("bar", 5L); final GsonFooCustomObjectDraft draft = new GsonFooCustomObjectDraft("container", "key", value); final GsonFooCustomObjectUpsertCommand command = new GsonFooCustomObjectUpsertCommand(draft); final CustomObject<GsonFoo> customObject = client().executeBlocking(command); final GsonFoo actualValue = customObject.getValue(); assertThat(actualValue).isEqualTo(value);
See the test code.
The command class:
import com.google.gson.Gson;
import io.sphere.sdk.client.SphereRequestUtils;
import io.sphere.sdk.customobjects.commands.CustomObjectCustomJsonMappingUpsertCommand;
import io.sphere.sdk.http.HttpResponse;
public class GsonFooCustomObjectUpsertCommand extends CustomObjectCustomJsonMappingUpsertCommand<GsonFoo> { private final Gson gson = new Gson(); private final GsonFooCustomObjectDraft draft; public GsonFooCustomObjectUpsertCommand(final GsonFooCustomObjectDraft draft) { this.draft = draft; } //used to send the value of the object @Override protected String bodyAsJsonString() { return gson.toJson(draft); } //used to deserialize the response @Override public GsonFooCustomObject deserialize(final HttpResponse httpResponse) { final String jsonAsString = SphereRequestUtils.getBodyAsString(httpResponse); return gson.fromJson(jsonAsString, GsonFooCustomObject.class); } }
See the test code.
The draft containing container, key and value:
public class GsonFooCustomObjectDraft { private final GsonFoo value; private final String container; private final String key; public GsonFooCustomObjectDraft(final String container, final String key, final GsonFoo value) { this.container = container; this.value = value; this.key = key; } }
See the test code.
The resulting custom object class:
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 class of the 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.
CustomObject
Modifier | Constructor and Description |
---|---|
protected |
CustomObjectCustomJsonMappingUpsertCommand() |
Modifier and Type | Method and Description |
---|---|
protected abstract String |
bodyAsJsonString()
Produces JSON to create or update a custom object.
|
abstract CustomObject<T> |
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.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
canDeserialize
protected CustomObjectCustomJsonMappingUpsertCommand()
public abstract CustomObject<T> deserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.canDeserialize(HttpResponse)
if the response can be consumed.deserialize
in interface SphereRequest<CustomObject<T>>
httpResponse
- the http response of the APIpublic final HttpRequestIntent httpRequestIntent()
SphereRequest
httpRequestIntent
in interface SphereRequest<CustomObject<T>>
protected abstract String bodyAsJsonString()
CustomObject
:
Example:
{
"container": "myNamespace",
"key": "myKey",
"value": {
"baz": 3,
"bar": "a String"
}
}