Move JsonMapper to spring-cloud-function-context

...in case it is needed elsewhere (see gh-151)
This commit is contained in:
Dave Syer
2018-05-21 08:50:35 +01:00
parent a64579dbdb
commit 66476559ed
8 changed files with 96 additions and 58 deletions

View File

@@ -32,6 +32,16 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -39,6 +39,9 @@ import java.util.function.Supplier;
import javax.annotation.PreDestroy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -46,7 +49,10 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionRegistry;
@@ -62,8 +68,11 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.cloud.function.core.IsolatedConsumer;
import org.springframework.cloud.function.core.IsolatedFunction;
import org.springframework.cloud.function.core.IsolatedSupplier;
import org.springframework.cloud.function.json.GsonMapper;
import org.springframework.cloud.function.json.JacksonMapper;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -86,6 +95,8 @@ import org.springframework.util.StringUtils;
@ConditionalOnMissingBean(FunctionCatalog.class)
public class ContextFunctionCatalogAutoConfiguration {
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
@Autowired(required = false)
private Map<String, Supplier<?>> suppliers = Collections.emptyMap();
@@ -169,6 +180,26 @@ public class ContextFunctionCatalogAutoConfiguration {
}
@Configuration
@ConditionalOnClass(Gson.class)
@Conditional(PreferGsonOrMissingJacksonCondition.class)
protected static class GsonConfiguration {
@Bean
public GsonMapper jsonMapper(Gson gson) {
return new GsonMapper(gson);
}
}
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@ConditionalOnProperty(name = ContextFunctionCatalogAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jackson", matchIfMissing = true)
protected static class JacksonConfiguration {
@Bean
public JacksonMapper jsonMapper(ObjectMapper mapper) {
return new JacksonMapper(mapper);
}
}
@Component
protected static class ContextFunctionRegistry {
@@ -643,4 +674,22 @@ public class ContextFunctionCatalogAutoConfiguration {
}
private static class PreferGsonOrMissingJacksonCondition extends AnyNestedCondition {
PreferGsonOrMissingJacksonCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(name = ContextFunctionCatalogAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson", matchIfMissing = false)
static class GsonPreferred {
}
@ConditionalOnMissingBean(ObjectMapper.class)
static class JacksonMissing {
}
}
}

View File

@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web.util;
package org.springframework.cloud.function.json;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.core.ResolvableType;
/**
@@ -45,4 +46,8 @@ public class GsonMapper implements JsonMapper {
return gson.fromJson(json, type);
}
@Override
public String toString(Object value) {
return gson.toJson(value);
}
}

View File

@@ -13,13 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web.util;
package org.springframework.cloud.function.json;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.function.json.JsonMapper;
/**
* @author Dave Syer
*
@@ -53,4 +56,13 @@ public class JacksonMapper implements JsonMapper {
}
}
@Override
public String toString(Object value) {
try {
return mapper.writeValueAsString(value);
}
catch (JsonProcessingException e) {
throw new IllegalArgumentException("Cannot convert to JSON", e);
}
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web.util;
package org.springframework.cloud.function.json;
import java.util.List;
@@ -27,4 +27,6 @@ public interface JsonMapper {
<T> T toSingle(String json, Class<T> type);
String toString(Object value);
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web.util;
package org.springframework.cloud.function.util;
import java.util.Arrays;
import java.util.List;
@@ -26,6 +26,10 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.cloud.function.json.GsonMapper;
import org.springframework.cloud.function.json.JacksonMapper;
import org.springframework.cloud.function.json.JsonMapper;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -33,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
*/
@RunWith(Parameterized.class)
public class MapperTests {
public class JsonMapperTests {
private JsonMapper mapper;
@@ -43,16 +47,18 @@ public class MapperTests {
new Object[] { new JacksonMapper(new ObjectMapper()) });
}
public MapperTests(JsonMapper mapper) {
public JsonMapperTests(JsonMapper mapper) {
this.mapper = mapper;
}
@Test
public void vanillaArray() {
List<Foo> list = mapper.toList("[{\"value\":\"foo\"}, {\"value\":\"foo\"}]",
String json = "[{\"value\":\"foo\"},{\"value\":\"foo\"}]";
List<Foo> list = mapper.toList(json,
Foo.class);
assertThat(list).hasSize(2);
assertThat(list.get(0).getValue()).isEqualTo("foo");
assertThat(mapper.toString(list)).isEqualTo(json);
}
@Test
@@ -70,8 +76,10 @@ public class MapperTests {
@Test
public void vanillaObject() {
Foo foo = mapper.toSingle("{\"value\":\"foo\"}", Foo.class);
String json = "{\"value\":\"foo\"}";
Foo foo = mapper.toSingle(json, Foo.class);
assertThat(foo.getValue()).isEqualTo("foo");
assertThat(mapper.toString(foo)).isEqualTo(json);
}
@Test

View File

@@ -19,32 +19,24 @@ package org.springframework.cloud.function.web.flux;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.cloud.function.web.flux.request.FluxHandlerMethodArgumentResolver;
import org.springframework.cloud.function.web.flux.response.FluxReturnValueHandler;
import org.springframework.cloud.function.web.util.GsonMapper;
import org.springframework.cloud.function.web.util.JacksonMapper;
import org.springframework.cloud.function.web.util.JsonMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.ConversionService;
@@ -71,8 +63,6 @@ public class ReactorAutoConfiguration {
@Autowired
private ApplicationContext context;
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
@Bean
public FunctionHandlerMapping functionHandlerMapping(FunctionCatalog catalog,
FunctionController controller) {
@@ -129,44 +119,6 @@ public class ReactorAutoConfiguration {
};
}
@Configuration
@ConditionalOnClass(Gson.class)
@Conditional(PreferGsonOrMissingJacksonCondition.class)
protected static class GsonConfiguration {
@Bean
public GsonMapper jsonMapper(Gson gson) {
return new GsonMapper(gson);
}
}
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@ConditionalOnProperty(name = ReactorAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "jackson", matchIfMissing = true)
protected static class JacksonConfiguration {
@Bean
public JacksonMapper jsonMapper(ObjectMapper mapper) {
return new JacksonMapper(mapper);
}
}
private static class PreferGsonOrMissingJacksonCondition extends AnyNestedCondition {
PreferGsonOrMissingJacksonCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(name = ReactorAutoConfiguration.PREFERRED_MAPPER_PROPERTY, havingValue = "gson", matchIfMissing = false)
static class GsonPreferred {
}
@ConditionalOnMissingBean(ObjectMapper.class)
static class JacksonMissing {
}
}
private static class BasicStringConverter implements StringConverter {
private ConversionService conversionService;

View File

@@ -28,9 +28,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.context.message.MessageUtils;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.cloud.function.web.flux.constants.WebRequestConstants;
import org.springframework.cloud.function.web.util.HeaderUtils;
import org.springframework.cloud.function.web.util.JsonMapper;
import org.springframework.core.MethodParameter;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;