Skip to content

Commit

Permalink
Allow providing a customized ObjectMapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
arucard21 committed Feb 18, 2020
1 parent e7b46b3 commit 070f9f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

import io.openapitools.jackson.dataformat.hal.HALLink;
import io.openapitools.jackson.dataformat.hal.HALMapper;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand Down Expand Up @@ -56,13 +57,15 @@ public class SimplyRESTfulClient<T extends HALResource> {
private final String resourceProfile;
private final MediaType resourceMediaType;
private final Client client;
private final ObjectMapper halMapper;

SimplyRESTfulClient(Client client, URI baseApiUri, Class<T> resourceClass) {
SimplyRESTfulClient(Client client, ObjectMapper halMapper, URI baseApiUri, Class<T> resourceClass) {
this.baseApiUri = baseApiUri;
this.client = client;
if (!client.getConfiguration().isRegistered(JacksonJsonProvider.class)) {
client.register(new JacksonJsonProvider(new HALMapper()));
client.register(new JacksonJsonProvider(halMapper));
}
this.halMapper = halMapper;
this.resourceClass = resourceClass;
this.resourceProfile = discoverResourceProfile();
this.resourceMediaType = detectResourceMediaType();
Expand Down Expand Up @@ -196,15 +199,17 @@ private HALCollection<T> retrievePagedCollection(int page, int pageSize, boolean

private <S> S deserializeJson(String jsonString, Class<S> deserializationClass) {
try {
return new HALMapper().readValue(jsonString, deserializationClass);
ObjectMapper mapper = halMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(jsonString, deserializationClass);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private <S> S deserializeJsonWithGenerics(String jsonString, TypeReference<S> typeRef) {
try {
return new HALMapper().readValue(jsonString, typeRef);
ObjectMapper mapper = halMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(jsonString, typeRef);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
import javax.inject.Named;
import javax.ws.rs.client.Client;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.openapitools.jackson.dataformat.hal.HALMapper;
import simplyrestful.api.framework.resources.HALResource;

@Named
public class SimplyRESTfulClientFactory<T extends HALResource> {
private final Client client;
private ObjectMapper halMapper;

@Inject
public SimplyRESTfulClientFactory(Client client) {
this.client = client;
this.halMapper = new HALMapper();
}

public SimplyRESTfulClientFactory<T> withMapper(ObjectMapper halMapper){
this.halMapper = halMapper;
return this;
}

public SimplyRESTfulClient<T> newClient(URI baseApiUri, Class<T> resourceClass){
return new SimplyRESTfulClient<T>(client, baseApiUri, resourceClass);
return new SimplyRESTfulClient<T>(client, halMapper, baseApiUri, resourceClass);
}
}

0 comments on commit 070f9f4

Please sign in to comment.