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

@@ -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;

View File

@@ -1,48 +0,0 @@
/*
* 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.cloud.function.web.util;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import org.springframework.core.ResolvableType;
/**
* @author Dave Syer
*
*/
public class GsonMapper implements JsonMapper {
private final Gson gson;
public GsonMapper(Gson gson) {
this.gson = gson;
}
@Override
public <T> List<T> toList(String json, Class<T> type) {
return gson.fromJson(json,
ResolvableType.forClassWithGenerics(ArrayList.class, type).getType());
}
@Override
public <T> T toSingle(String json, Class<T> type) {
return gson.fromJson(json, type);
}
}

View File

@@ -1,56 +0,0 @@
/*
* 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.cloud.function.web.util;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Dave Syer
*
*/
public class JacksonMapper implements JsonMapper {
private final ObjectMapper mapper;
public JacksonMapper(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public <T> List<T> toList(String json, Class<T> type) {
try {
return mapper.readValue(json, mapper.getTypeFactory()
.constructCollectionLikeType(ArrayList.class, type));
}
catch (Exception e) {
throw new IllegalArgumentException("Cannot convert JSON", e);
}
}
@Override
public <T> T toSingle(String json, Class<T> type) {
try {
return mapper.readValue(json, type);
}
catch (Exception e) {
throw new IllegalArgumentException("Cannot convert JSON", e);
}
}
}

View File

@@ -1,30 +0,0 @@
/*
* 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.cloud.function.web.util;
import java.util.List;
/**
* @author Dave Syer
*
*/
public interface JsonMapper {
<T> List<T> toList(String json, Class<T> type);
<T> T toSingle(String json, Class<T> type);
}

View File

@@ -1,101 +0,0 @@
/*
* 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.cloud.function.web.util;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
@RunWith(Parameterized.class)
public class MapperTests {
private JsonMapper mapper;
@Parameters
public static List<Object[]> params() {
return Arrays.asList(new Object[] { new GsonMapper(new Gson()) },
new Object[] { new JacksonMapper(new ObjectMapper()) });
}
public MapperTests(JsonMapper mapper) {
this.mapper = mapper;
}
@Test
public void vanillaArray() {
List<Foo> list = mapper.toList("[{\"value\":\"foo\"}, {\"value\":\"foo\"}]",
Foo.class);
assertThat(list).hasSize(2);
assertThat(list.get(0).getValue()).isEqualTo("foo");
}
@Test
public void intArray() {
List<Integer> list = mapper.toList("[123,456]", Integer.class);
assertThat(list).hasSize(2);
assertThat(list.get(0)).isEqualTo(123);
}
@Test
public void emptyArray() {
List<Foo> list = mapper.toList("[]", Foo.class);
assertThat(list).hasSize(0);
}
@Test
public void vanillaObject() {
Foo foo = mapper.toSingle("{\"value\":\"foo\"}", Foo.class);
assertThat(foo.getValue()).isEqualTo("foo");
}
@Test
public void intValue() {
int foo = mapper.toSingle("123", Integer.class);
assertThat(foo).isEqualTo(123);
}
@Test
public void empty() {
Foo foo = mapper.toSingle("{}", Foo.class);
assertThat(foo.getValue()).isNull();
}
public static class Foo {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}