Centralize jackson configuration.

This commit is contained in:
Scott Frederick
2017-05-19 14:01:03 -05:00
parent ea38f690fb
commit 06d4003f66
12 changed files with 72 additions and 59 deletions

View File

@@ -21,9 +21,6 @@ package org.springframework.credhub.core;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
@@ -77,13 +74,10 @@ public class CredHubClient {
* @return the list of {@link HttpMessageConverter}s
*/
private static List<HttpMessageConverter<?>> createMessageConverters() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new ISO8601DateFormat());
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(3);
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
messageConverters.add(new MappingJackson2HttpMessageConverter(JsonUtils.buildObjectMapper()));
return messageConverters;
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.credhub.core;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.springframework.credhub.support.JsonCredential;
import org.springframework.credhub.support.ValueType;
/**
* Utility methods for configuring JSON serialization and deserialization.
*
* @author Scott Frederick
*/
public class JsonUtils {
/**
* Create and configure the {@link ObjectMapper} used for serializing and deserializing
* JSON requests and responses.
*
* @return a configured {@link ObjectMapper}
*/
public static ObjectMapper buildObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new ISO8601DateFormat());
objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
configureCredentialDetailValueTypeMapping(objectMapper);
return objectMapper;
}
/**
* Configure type mapping for the {@literal value} field in the {@literal CredentialDetails}
* object.
*
* @param objectMapper the {@link ObjectMapper} to configure
*/
private static void configureCredentialDetailValueTypeMapping(ObjectMapper objectMapper) {
objectMapper.registerSubtypes(
new NamedType(String.class, ValueType.PASSWORD.type()),
new NamedType(JsonCredential.class, ValueType.JSON.type())
);
}
}

View File

@@ -16,13 +16,8 @@
package org.springframework.credhub.support;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
/**
* The details of a credential that has been written to CredHub. Clients don't
@@ -32,8 +27,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming;
*
* @author Scott Frederick
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CredentialDetails<T> extends CredentialSummary {
private String id;
@@ -41,10 +34,6 @@ public class CredentialDetails<T> extends CredentialSummary {
private ValueType valueType;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = String.class, name = "password"),
@Type(value = JsonCredential.class, name = "json")
})
private T value;
/**

View File

@@ -21,11 +21,6 @@ package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
/**
* A collection of {@link CredentialDetails}. Clients don't typically instantiate
* objects of this type, but will receive them in response to write and retrieve
@@ -33,8 +28,6 @@ import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
*
* @author Scott Frederick
*/
@JsonInclude(NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CredentialDetailsData<T> {
private List<CredentialDetails<T>> data;

View File

@@ -18,10 +18,6 @@ package org.springframework.credhub.support;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
/**
* A summary of a credential that has been written to CredHub. Clients don't typically
* instantiate objects of this type, but will receive them in response to write and
@@ -29,8 +25,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming;
*
* @author Scott Frederick
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CredentialSummary {
protected CredentialName name;
protected Date versionCreatedAt;

View File

@@ -19,10 +19,6 @@ package org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
/**
* A collection of {@link CredentialSummary}s. Clients don't typically instantiate
* objects of this type, but will receive them in response to write and retrieve
@@ -30,8 +26,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming;
*
* @author Scott Frederick
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CredentialSummaryData {
private List<CredentialSummary> credentials;

View File

@@ -16,9 +16,6 @@
package org.springframework.credhub.support;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.springframework.util.Assert;
import java.util.Map;
@@ -30,7 +27,6 @@ import static org.springframework.credhub.support.ValueType.JSON;
*
* @author Scott Frederick
*/
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class JsonWriteRequest extends WriteRequest<JsonCredential> {
/**
* Create a builder that provides a fluent API for providing the values required

View File

@@ -16,9 +16,6 @@
package org.springframework.credhub.support;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.springframework.util.Assert;
import static org.springframework.credhub.support.ValueType.PASSWORD;
@@ -28,7 +25,6 @@ import static org.springframework.credhub.support.ValueType.PASSWORD;
*
* @author Scott Frederick
*/
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PasswordWriteRequest extends WriteRequest<String> {
/**
* Create a builder that provides a fluent API for providing the values required

View File

@@ -24,25 +24,19 @@ import java.util.Collection;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.springframework.util.Assert;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
/**
* The details of a request to write a new or update an existing credential in CredHub.
*
* @author Scott Frederick
*/
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class WriteRequest<T> {
private boolean overwrite;
private CredentialName name;
private ValueType valueType;
private T value;
@JsonInclude(NON_EMPTY)
private List<AdditionalPermission> additionalPermissions;
/**

View File

@@ -94,7 +94,7 @@ public class CredHubTemplateUnitTests extends CredHubTemplateUnitTestsBase {
" ]" +
"}";
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = JsonUtils.buildObjectMapper();
return mapper.readValue(vcapServices, VcapServicesData.class);
}

View File

@@ -16,12 +16,12 @@
package org.springframework.credhub.support;
import java.text.DateFormat;
import java.util.Date;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.junit.Before;
import org.springframework.credhub.core.JsonUtils;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -47,12 +47,9 @@ public abstract class JsonParsingUnitTestsBase {
@Before
public void setUpJsonParsing() throws Exception {
DateFormat dateFormat = new ISO8601DateFormat();
objectMapper = JsonUtils.buildObjectMapper();
objectMapper = new ObjectMapper();
objectMapper.setDateFormat(dateFormat);
testDate = dateFormat.parse(TEST_DATE_STRING);
testDate = new ISO8601DateFormat().parse(TEST_DATE_STRING);
}

View File

@@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.springframework.credhub.core.JsonUtils;
import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder;
import static org.hamcrest.CoreMatchers.allOf;
@@ -37,7 +38,7 @@ public abstract class WriteRequestUnitTestsBase {
@Before
public void setUpWriteRequestUnitTestsBase() {
mapper = new ObjectMapper();
mapper = JsonUtils.buildObjectMapper();
}
@Test