Browse Source

Accept JsonProvider upon JsonProjectingMethodInterceptorFactory creation.

We now configure explicitly the JsonProvider that is used by JSONPath through JsonProjectingMethodInterceptorFactory to avoid misconfiguration due to JSONPath defaulting attempts. Accepting JsonProvider allows for improved flexibility during configuration.

Closes #2403
pull/2456/head
sokomishalov 5 years ago committed by Mark Paluch
parent
commit
d1e27b55e2
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 38
      src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java
  2. 3
      src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java
  3. 5
      src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java

38
src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java

@ -38,7 +38,6 @@ import org.springframework.data.util.TypeInformation; @@ -38,7 +38,6 @@ import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
@ -47,12 +46,14 @@ import com.jayway.jsonpath.ParseContext; @@ -47,12 +46,14 @@ import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
/**
* {@link MethodInterceptorFactory} to create a {@link MethodInterceptor} that will
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Mikhael Sokolov
* @soundtrack Jeff Coffin - Fruitcake (The Inside Of The Outside)
* @since 1.13
*/
@ -60,21 +61,34 @@ public class JsonProjectingMethodInterceptorFactory implements MethodInterceptor @@ -60,21 +61,34 @@ public class JsonProjectingMethodInterceptorFactory implements MethodInterceptor
private final ParseContext context;
/**
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider} and {@link JsonProvider}.
*
* @param mappingProvider must not be {@literal null}.
* @param jsonProvider must not be {@literal null}.
*/
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider, JsonProvider jsonProvider) {
Assert.notNull(mappingProvider, "MappingProvider must not be null!");
Assert.notNull(jsonProvider, "JsonProvider must not be null!");
Configuration configuration = Configuration.builder()//
.options(Option.ALWAYS_RETURN_LIST)//
.mappingProvider(mappingProvider)//
.jsonProvider(jsonProvider)//
.build();
this.context = JsonPath.using(configuration);
}
/**
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link ObjectMapper}.
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider}.
*
* @param mapper must not be {@literal null}.
* @param mappingProvider must not be {@literal null}.
*/
@Deprecated
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider) {
Assert.notNull(mappingProvider, "MappingProvider must not be null!");
Configuration build = Configuration.builder()//
.options(Option.ALWAYS_RETURN_LIST)//
.mappingProvider(mappingProvider)//
.build();
this.context = JsonPath.using(build);
this(mappingProvider, Configuration.defaultConfiguration().jsonProvider());
}
/*

3
src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java

@ -19,6 +19,7 @@ import java.io.IOException; @@ -19,6 +19,7 @@ import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
@ -85,7 +86,7 @@ public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpM @@ -85,7 +86,7 @@ public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpM
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
projectionFactory
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper)));
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper), new JacksonJsonProvider(mapper)));
return projectionFactory;
}

5
src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java

@ -17,6 +17,8 @@ package org.springframework.data.web; @@ -17,6 +17,8 @@ package org.springframework.data.web;
import static org.assertj.core.api.Assertions.*;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -59,7 +61,8 @@ class JsonProjectingMethodInterceptorFactoryUnitTests { @@ -59,7 +61,8 @@ class JsonProjectingMethodInterceptorFactoryUnitTests {
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
MappingProvider mappingProvider = new JacksonMappingProvider(new ObjectMapper());
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider));
JsonProvider jsonProvider = new JacksonJsonProvider(new ObjectMapper());
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider, jsonProvider));
this.projectionFactory = projectionFactory;
this.customer = projectionFactory.createProjection(Customer.class, new ByteArrayInputStream(json.getBytes()));

Loading…
Cancel
Save