Move JsonMapper to spring-cloud-function-context
...in case it is needed elsewhere (see gh-151)
This commit is contained in:
@@ -32,6 +32,16 @@
|
|||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ import java.util.function.Supplier;
|
|||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
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.config.ConstructorArgumentValues.ValueHolder;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
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.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||||
import org.springframework.cloud.function.context.FunctionRegistration;
|
import org.springframework.cloud.function.context.FunctionRegistration;
|
||||||
import org.springframework.cloud.function.context.FunctionRegistry;
|
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.IsolatedConsumer;
|
||||||
import org.springframework.cloud.function.core.IsolatedFunction;
|
import org.springframework.cloud.function.core.IsolatedFunction;
|
||||||
import org.springframework.cloud.function.core.IsolatedSupplier;
|
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.ApplicationEventPublisher;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Conditional;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
@@ -86,6 +95,8 @@ import org.springframework.util.StringUtils;
|
|||||||
@ConditionalOnMissingBean(FunctionCatalog.class)
|
@ConditionalOnMissingBean(FunctionCatalog.class)
|
||||||
public class ContextFunctionCatalogAutoConfiguration {
|
public class ContextFunctionCatalogAutoConfiguration {
|
||||||
|
|
||||||
|
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private Map<String, Supplier<?>> suppliers = Collections.emptyMap();
|
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
|
@Component
|
||||||
protected static class ContextFunctionRegistry {
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,14 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.cloud.function.web.util;
|
package org.springframework.cloud.function.json;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import org.springframework.cloud.function.json.JsonMapper;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,4 +46,8 @@ public class GsonMapper implements JsonMapper {
|
|||||||
return gson.fromJson(json, type);
|
return gson.fromJson(json, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(Object value) {
|
||||||
|
return gson.toJson(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,13 +13,16 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.cloud.function.web.util;
|
package org.springframework.cloud.function.json;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import org.springframework.cloud.function.json.JsonMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Dave Syer
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.cloud.function.web.util;
|
package org.springframework.cloud.function.json;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -27,4 +27,6 @@ public interface JsonMapper {
|
|||||||
|
|
||||||
<T> T toSingle(String json, Class<T> type);
|
<T> T toSingle(String json, Class<T> type);
|
||||||
|
|
||||||
|
String toString(Object value);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.springframework.cloud.function.web.util;
|
package org.springframework.cloud.function.util;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -26,6 +26,10 @@ import org.junit.runner.RunWith;
|
|||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
import org.junit.runners.Parameterized.Parameters;
|
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;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
public class MapperTests {
|
public class JsonMapperTests {
|
||||||
|
|
||||||
private JsonMapper mapper;
|
private JsonMapper mapper;
|
||||||
|
|
||||||
@@ -43,16 +47,18 @@ public class MapperTests {
|
|||||||
new Object[] { new JacksonMapper(new ObjectMapper()) });
|
new Object[] { new JacksonMapper(new ObjectMapper()) });
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapperTests(JsonMapper mapper) {
|
public JsonMapperTests(JsonMapper mapper) {
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void vanillaArray() {
|
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);
|
Foo.class);
|
||||||
assertThat(list).hasSize(2);
|
assertThat(list).hasSize(2);
|
||||||
assertThat(list.get(0).getValue()).isEqualTo("foo");
|
assertThat(list.get(0).getValue()).isEqualTo("foo");
|
||||||
|
assertThat(mapper.toString(list)).isEqualTo(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -70,8 +76,10 @@ public class MapperTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void vanillaObject() {
|
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(foo.getValue()).isEqualTo("foo");
|
||||||
|
assertThat(mapper.toString(foo)).isEqualTo(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -19,32 +19,24 @@ package org.springframework.cloud.function.web.flux;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.SmartInitializingSingleton;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
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.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
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.condition.ConditionalOnWebApplication;
|
||||||
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
|
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
|
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
|
||||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
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.request.FluxHandlerMethodArgumentResolver;
|
||||||
import org.springframework.cloud.function.web.flux.response.FluxReturnValueHandler;
|
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.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
@@ -71,8 +63,6 @@ public class ReactorAutoConfiguration {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
||||||
|
|
||||||
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public FunctionHandlerMapping functionHandlerMapping(FunctionCatalog catalog,
|
public FunctionHandlerMapping functionHandlerMapping(FunctionCatalog catalog,
|
||||||
FunctionController controller) {
|
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 static class BasicStringConverter implements StringConverter {
|
||||||
|
|
||||||
private ConversionService conversionService;
|
private ConversionService conversionService;
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
|
|
||||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||||
import org.springframework.cloud.function.context.message.MessageUtils;
|
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.flux.constants.WebRequestConstants;
|
||||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
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.MethodParameter;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
|||||||
Reference in New Issue
Block a user