Commit 429cd8d1 authored by Stephane Nicoll's avatar Stephane Nicoll

Optimize use of Jackson ObjectMapper instances

Closes gh-1789
parent 60eace17
......@@ -79,6 +79,8 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
private ApplicationContext context;
private ObjectMapper objectMapper;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
......@@ -94,13 +96,11 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
}
private ApplicationConfigurationProperties extract(ApplicationContext context) {
ObjectMapper mapper = new ObjectMapper();
configureObjectMapper(mapper);
Map<String, ContextConfigurationProperties> contextProperties = new HashMap<>();
ApplicationContext target = context;
while (target != null) {
contextProperties.put(target.getId(),
describeConfigurationProperties(target, mapper));
describeConfigurationProperties(target, getObjectMapper()));
target = target.getParent();
}
return new ApplicationConfigurationProperties(contextProperties);
......@@ -179,6 +179,14 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
applySerializationModifier(mapper);
}
private ObjectMapper getObjectMapper() {
if (this.objectMapper == null) {
this.objectMapper = new ObjectMapper();
configureObjectMapper(this.objectMapper);
}
return this.objectMapper;
}
/**
* Ensure only bindable and non-cyclic bean properties are reported.
* @param mapper the object mapper
......
......@@ -36,6 +36,20 @@ public class JacksonJsonParser extends AbstractJsonParser {
private ObjectMapper objectMapper; // Late binding
/**
* Creates a instance with the specified {@link ObjectMapper}.
* @param objectMapper the object mapper to use
*/
public JacksonJsonParser(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
* Creates an instance with a default {@link ObjectMapper} that is created lazily.
*/
public JacksonJsonParser() {
}
@Override
public Map<String, Object> parseMap(String json) {
return tryParse(() -> getObjectMapper().readValue(json, MAP_TYPE),
......
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
......@@ -16,10 +16,22 @@
package org.springframework.boot.json;
import java.io.IOException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link JacksonJsonParser}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public class JacksonJsonParserTests extends AbstractJsonParserTests {
......@@ -28,4 +40,11 @@ public class JacksonJsonParserTests extends AbstractJsonParserTests {
return new JacksonJsonParser();
}
@Test
public void instanceWithSpecificObjectMapper() throws IOException {
ObjectMapper objectMapper = spy(new ObjectMapper());
new JacksonJsonParser(objectMapper).parseMap("{}");
verify(objectMapper).readValue(eq("{}"), any(TypeReference.class));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment