DATAREST-621 - Introduced RepositoryRestConfigurer for less invasive customization of Spring Data REST.

We now expose a RepositoryRestConfigurer interface that carries all configure… methods that exist on RepositoryRestMvcConfiguration. The latter are deprecated as of now in favor of implementing and declaring a RepositoryRestConfigurer bean (or extending RepositoryRestConfigurerAdapter for convenience).

Moved extensions of RepositoryRestMvcConfiguration to RepositoryRestConfigurerAdapter where possible.
This commit is contained in:
Oliver Gierke
2015-07-21 12:53:31 +02:00
parent 1e9e3f5635
commit 12121f842e
7 changed files with 369 additions and 43 deletions

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2015 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.data.rest.webmvc.config;
import java.util.List;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Component to configure and customize the setup of Spring Data REST.
*
* @author Oliver Gierke
* @since 2.4
* @soundtrack Florian Reichelt & Max Ender - Abschlusskonzert (https://www.youtube.com/watch?v=5WP0P-ndinY)
*/
public interface RepositoryRestConfigurer {
/**
* Override this method to add additional configuration.
*
* @param config Main configuration bean.
*/
void configureRepositoryRestConfiguration(RepositoryRestConfiguration config);
/**
* Override this method to add your own converters.
*
* @param conversionService Default ConversionService bean.
*/
void configureConversionService(ConfigurableConversionService conversionService);
/**
* Override this method to add validators manually.
*
* @param validatingListener The {@link org.springframework.context.ApplicationListener} responsible for invoking
* {@link org.springframework.validation.Validator} instances.
*/
void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener);
/**
* Configure the {@link ExceptionHandlerExceptionResolver}.
*
* @param exceptionResolver The default exception resolver on which you can add custom argument resolvers.
*/
void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver);
/**
* Configure the available {@link HttpMessageConverter}s by adding your own.
*
* @param messageConverters The converters to be used by the system.
*/
void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters);
/**
* Configure the Jackson {@link ObjectMapper} directly.
*
* @param objectMapper The {@literal ObjectMapper} to be used by the system.
*/
void configureJacksonObjectMapper(ObjectMapper objectMapper);
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2015 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.data.rest.webmvc.config;
import java.util.List;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Convenience adapter for {@link RepositoryRestConfigurer} to allow selective override of configuration methods.
*
* @author Oliver Gierke
* @since 2.4
* @soundtrack Florian Reichelt & Max Ender - Abschlusskonzert (https://www.youtube.com/watch?v=5WP0P-ndinY)
*/
public class RepositoryRestConfigurerAdapter implements RepositoryRestConfigurer {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)
*/
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureConversionService(org.springframework.core.convert.support.ConfigurableConversionService)
*/
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureValidatingRepositoryEventListener(org.springframework.data.rest.core.event.ValidatingRepositoryEventListener)
*/
@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)
*/
@Override
public void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureHttpMessageConverters(java.util.List)
*/
@Override
public void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureJacksonObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)
*/
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2015 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.data.rest.webmvc.config;
import java.util.List;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Delegating implementation of {@link RepositoryRestConfigurer} that will forward all calls to configuration methods to
* all registered {@link RepositoryRestConfigurer}.
*
* @author Oliver Gierke
* @soundtrack Florian Reichelt & Max Ender - Abschlusskonzert (https://www.youtube.com/watch?v=5WP0P-ndinY)
*/
class RepositoryRestConfigurerDelegate implements RepositoryRestConfigurer {
private final Iterable<RepositoryRestConfigurer> delegates;
/**
* Creates a new {@link RepositoryRestConfigurerDelegate} for the given {@link RepositoryRestConfigurer}s.
*
* @param delegates must not be {@literal null}.
*/
public RepositoryRestConfigurerDelegate(Iterable<RepositoryRestConfigurer> delegates) {
Assert.notNull(delegates, "RepositoryRestConfigurers must not be null!");
this.delegates = delegates;
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)
*/
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureRepositoryRestConfiguration(config);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureConversionService(org.springframework.core.convert.support.ConfigurableConversionService)
*/
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureConversionService(conversionService);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver)
*/
@Override
public void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureExceptionHandlerExceptionResolver(exceptionResolver);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureHttpMessageConverters(java.util.List)
*/
@Override
public void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureHttpMessageConverters(messageConverters);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureJacksonObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)
*/
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureJacksonObjectMapper(objectMapper);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureValidatingRepositoryEventListener(org.springframework.data.rest.core.event.ValidatingRepositoryEventListener)
*/
@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
for (RepositoryRestConfigurer configurer : delegates) {
configurer.configureValidatingRepositoryEventListener(validatingListener);
}
}
}

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -145,7 +146,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
includeFilters = @Filter(BasePathAwareController.class) , useDefaultFilters = false)
@ImportResource("classpath*:META-INF/spring-data-rest/**/*.xml")
@Import(SpringDataJacksonConfiguration.class)
public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebConfiguration {
public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebConfiguration implements InitializingBean {
private static final boolean IS_JPA_AVAILABLE = ClassUtils.isPresent("javax.persistence.EntityManager",
RepositoryRestMvcConfiguration.class.getClassLoader());
@@ -153,10 +154,22 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Autowired ApplicationContext applicationContext;
@Autowired(required = false) List<BackendIdConverter> idConverters = Collections.emptyList();
@Autowired(required = false) List<RepositoryRestConfigurer> configurers = Collections.emptyList();
@Autowired(required = false) RelProvider relProvider;
@Autowired(required = false) CurieProvider curieProvider;
private RepositoryRestConfigurerDelegate configurerDelegate;
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
this.configurerDelegate = new RepositoryRestConfigurerDelegate(configurers);
}
@Bean
public Repositories repositories() {
return new Repositories(applicationContext);
@@ -189,6 +202,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
conversionService.addConverter(uriToEntityConverter(conversionService));
addFormatters(conversionService);
configurerDelegate.configureConversionService(conversionService);
configureConversionService(conversionService);
return conversionService;
@@ -200,8 +214,11 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
*/
@Bean
public ValidatingRepositoryEventListener validatingRepositoryEventListener(ObjectFactory<Repositories> repositories) {
ValidatingRepositoryEventListener listener = new ValidatingRepositoryEventListener(repositories);
configurerDelegate.configureValidatingRepositoryEventListener(listener);
configureValidatingRepositoryEventListener(listener);
return listener;
}
@@ -228,6 +245,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
}
RepositoryRestConfiguration config = new RepositoryRestConfiguration(configuration, metadataConfiguration());
configurerDelegate.configureRepositoryRestConfiguration(config);
configureRepositoryRestConfiguration(config);
return config;
@@ -328,11 +346,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Bean
public PersistentEntityResourceHandlerMethodArgumentResolver persistentEntityArgumentResolver() {
List<HttpMessageConverter<?>> messageConverters = defaultMessageConverters();
configureHttpMessageConverters(messageConverters);
return new PersistentEntityResourceHandlerMethodArgumentResolver(messageConverters, repoRequestArgumentResolver(),
backendIdHandlerMethodArgumentResolver(), new DomainObjectReader(persistentEntities(), resourceMappings()));
return new PersistentEntityResourceHandlerMethodArgumentResolver(defaultMessageConverters(),
repoRequestArgumentResolver(), backendIdHandlerMethodArgumentResolver(),
new DomainObjectReader(persistentEntities(), resourceMappings()));
}
/**
@@ -473,11 +489,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
* @return
*/
@Bean
@SuppressWarnings("rawtypes")
public RequestMappingHandlerAdapter repositoryExporterHandlerAdapter() {
List<HttpMessageConverter<?>> messageConverters = defaultMessageConverters();
configureHttpMessageConverters(messageConverters);
Collection<ResourceProcessor> beans = applicationContext.getBeansOfType(ResourceProcessor.class, false, false)
.values();
List<ResourceProcessor<?>> processors = new ArrayList<ResourceProcessor<?>>(beans.size());
@@ -495,7 +509,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
RepositoryRestHandlerAdapter handlerAdapter = new RepositoryRestHandlerAdapter(defaultMethodArgumentResolvers(),
processors);
handlerAdapter.setWebBindingInitializer(initializer);
handlerAdapter.setMessageConverters(messageConverters);
handlerAdapter.setMessageConverters(defaultMessageConverters());
if (config().metadataConfiguration().alpsEnabled()) {
handlerAdapter.setResponseBodyAdvice(Arrays.<ResponseBodyAdvice<?>> asList(alpsJsonHttpMessageConverter()));
@@ -546,8 +560,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
PersistentEntities entities = persistentEntities();
return new PersistentEntityJackson2Module(
resourceMappings(), entities, config(), uriToEntityConverter(defaultConversionService()), entityLinks());
return new PersistentEntityJackson2Module(resourceMappings(), entities, config(),
uriToEntityConverter(defaultConversionService()), entityLinks());
}
/**
@@ -565,13 +579,12 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
*/
@Bean
public ExceptionHandlerExceptionResolver exceptionHandlerExceptionResolver() {
ExceptionHandlerExceptionResolver er = new ExceptionHandlerExceptionResolver();
er.setCustomArgumentResolvers(defaultMethodArgumentResolvers());
er.setMessageConverters(defaultMessageConverters());
List<HttpMessageConverter<?>> messageConverters = defaultMessageConverters();
configureHttpMessageConverters(messageConverters);
er.setMessageConverters(messageConverters);
configurerDelegate.configureExceptionHandlerExceptionResolver(er);
configureExceptionHandlerExceptionResolver(er);
return er;
@@ -611,6 +624,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
messageConverters.add(fallbackJsonConverter);
messageConverters.add(uriListHttpMessageConverter());
configurerDelegate.configureHttpMessageConverters(messageConverters);
configureHttpMessageConverters(messageConverters);
return messageConverters;
}
@@ -697,6 +713,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
Jackson2DatatypeHelper.configureObjectMapper(objectMapper);
// Configure custom Modules
configurerDelegate.configureJacksonObjectMapper(objectMapper);
configureJacksonObjectMapper(objectMapper);
return objectMapper;
@@ -754,7 +771,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
// Register HAL browser if present
if (ClassUtils.isPresent("org.springframework.data.rest.webmvc.halbrowser.HalBrowser", getClass().getClassLoader())) {
if (ClassUtils.isPresent("org.springframework.data.rest.webmvc.halbrowser.HalBrowser",
getClass().getClassLoader())) {
String basePath = config().getBasePath().toString().concat("/browser");
String rootLocation = "classpath:META-INF/spring-data-rest/hal-browser/";
@@ -792,14 +810,22 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
* Override this method to add additional configuration.
*
* @param config Main configuration bean.
* @deprecated since 2.4, implement
* {@link RepositoryRestConfigurer#configureRepositoryRestConfiguration(RepositoryRestConfiguration)}
* either directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {}
/**
* Override this method to add your own converters.
*
* @param conversionService Default ConversionService bean.
* @deprecated since 2.4, implement
* {@link RepositoryRestConfigurer#configureConversionService(ConfigurableConversionService)} either
* directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureConversionService(ConfigurableConversionService conversionService) {}
/**
@@ -807,27 +833,41 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
*
* @param validatingListener The {@link org.springframework.context.ApplicationListener} responsible for invoking
* {@link org.springframework.validation.Validator} instances.
* @deprecated since 2.4, implement
* {@link RepositoryRestConfigurer#configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener)}
* either directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {}
/**
* Configure the {@link ExceptionHandlerExceptionResolver}.
*
* @param exceptionResolver The default exception resolver on which you can add custom argument resolvers.
* @deprecated since 2.4, implement
* {@link RepositoryRestConfigurer#configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver)}
* either directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {}
/**
* Configure the available {@link HttpMessageConverter}s by adding your own.
*
* @param messageConverters The converters to be used by the system.
* @deprecated since 2.4, implement {@link RepositoryRestConfigurer#configureHttpMessageConverters(List)} either
* directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {}
/**
* Configure the Jackson {@link ObjectMapper} directly.
*
* @param objectMapper The {@literal ObjectMapper} to be used by the system.
* @deprecated since 2.4, implement {@link RepositoryRestConfigurer#configureJacksonObjectMapper(ObjectMapper)} either
* directly or extend {@link RepositoryRestConfigurerAdapter} and override the method.
*/
@Deprecated
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {}
}

View File

@@ -38,11 +38,11 @@ import org.springframework.test.context.ContextConfiguration;
public class LegacyRepresentationConfigIntegrationTests extends AbstractRepositoryRestMvcConfigurationIntegrationTests {
@Configuration
@Import(MongoDbRepositoryConfig.class)
static class Config extends RepositoryRestMvcConfiguration {
@Import({ MongoDbRepositoryConfig.class, RepositoryRestMvcConfiguration.class })
static class Config extends RepositoryRestConfigurerAdapter {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultMediaType(MediaType.APPLICATION_JSON);
config.useHalAsDefaultJsonMediaType(false);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 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.
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.PageRequest;
@@ -207,7 +208,8 @@ public class RepositoryRestMvConfigurationIntegrationTests {
}
@Configuration
static class ExtendingConfiguration extends RepositoryRestMvcConfiguration {
@Import(RepositoryRestMvcConfiguration.class)
static class ExtendingConfiguration extends RepositoryRestConfigurerAdapter {
@Bean
public DefaultRelProvider relProvider() {
@@ -220,7 +222,7 @@ public class RepositoryRestMvConfigurationIntegrationTests {
}
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(45);
config.setMaxPageSize(7000);
@@ -231,7 +233,8 @@ public class RepositoryRestMvConfigurationIntegrationTests {
}
@Configuration
static class NonHalConfiguration extends RepositoryRestMvcConfiguration {
@Import(RepositoryRestMvcConfiguration.class)
static class NonHalConfiguration extends RepositoryRestConfigurerAdapter {
@Bean
public CollectingComponent collectingComponent() {
@@ -239,7 +242,7 @@ public class RepositoryRestMvConfigurationIntegrationTests {
}
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.useHalAsDefaultJsonMediaType(false);
}
}

View File

@@ -28,12 +28,14 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.config.JsonSchemaFormat;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.RepositoryResourceMappings;
import org.springframework.data.rest.webmvc.TestMvcClient;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverterUnitTests.TestConfiguration;
import org.springframework.data.rest.webmvc.mongodb.MongoDbRepositoryConfig;
@@ -62,10 +64,11 @@ public class PersistentEntityToJsonSchemaConverterUnitTests {
@Autowired @Qualifier("objectMapper") ObjectMapper objectMapper;
@Configuration
static class TestConfiguration extends RepositoryRestMvcConfiguration {
@Import(RepositoryRestMvcConfiguration.class)
static class TestConfiguration extends RepositoryRestConfigurerAdapter {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.metadataConfiguration().registerJsonSchemaFormat(JsonSchemaFormat.EMAIL, EmailAddress.class);
config.metadataConfiguration().registerFormattingPatternFor("[A-Z]+", TypeWithPattern.class);
}
@@ -87,8 +90,8 @@ public class PersistentEntityToJsonSchemaConverterUnitTests {
List<Constraint> constraints = new ArrayList<Constraint>();
constraints.add(new Constraint("$.description", is("Profile description"), "Adds description to schema root"));
constraints.add(new Constraint("$.properties.renamed", is(notNullValue()), "Has descriptor for renamed property"));
constraints.add(new Constraint("$.properties.aliased", is(nullValue()),
"No descriptor for original name of renamed property"));
constraints.add(
new Constraint("$.properties.aliased", is(nullValue()), "No descriptor for original name of renamed property"));
assertConstraints(Profile.class, constraints);
}
@@ -98,29 +101,29 @@ public class PersistentEntityToJsonSchemaConverterUnitTests {
List<Constraint> constraints = new ArrayList<Constraint>();
constraints.add(new Constraint("$.properties.firstname.type", is("string"), "Exposes firstname as String"));
constraints.add(new Constraint("$.descriptors.address", is(notNullValue()),
"Exposes nested objects as descriptors."));
constraints
.add(new Constraint("$.descriptors.address", is(notNullValue()), "Exposes nested objects as descriptors."));
constraints.add(new Constraint("$.descriptors.address.type", is("object"), "Nested entity is of type 'object'"));
constraints.add(new Constraint("$.descriptors.address.properties.zipCode", is(notNullValue()),
"Exposes nested properties"));
constraints.add(new Constraint("$.descriptors.address.requiredProperties[0]", is("zipCode"),
"Lists nested required property"));
constraints.add(
new Constraint("$.descriptors.address.properties.zipCode", is(notNullValue()), "Exposes nested properties"));
constraints.add(
new Constraint("$.descriptors.address.requiredProperties[0]", is("zipCode"), "Lists nested required property"));
constraints.add(new Constraint("$.properties.gender.type", is("string"), "Enums are strings."));
constraints.add(new Constraint("$.properties.gender.enum", is(notNullValue()), "Exposes enum values."));
constraints.add(new Constraint("$.properties.jodaDateTime.format", is("date-time"),
"Exposes JodaTime dates in format."));
constraints.add(new Constraint("$.properties.java8DateTime.format", is("date-time"),
"Exposes Java 8 dates in format."));
constraints
.add(new Constraint("$.properties.jodaDateTime.format", is("date-time"), "Exposes JodaTime dates in format."));
constraints
.add(new Constraint("$.properties.java8DateTime.format", is("date-time"), "Exposes Java 8 dates in format."));
constraints.add(new Constraint("$.properties.nicknames.type", is("array"), "Exposes collection of simple types."));
constraints.add(new Constraint("$.properties.nicknames.items.type", is("string"),
"Exposes element type of collection of simple types."));
constraints.add(new Constraint("$.properties.email.format", is("email"), "Uses manually configured format."));
constraints.add(new Constraint("$.properties.email.type", is("string"), "Treats types with format as String."));
constraints.add(new Constraint("$.properties.shippingAddresses.type", is("array"),
"Exposes collection of complex types."));
constraints.add(new Constraint("$.properties.shippingAddresses.uniqueItems", is(true),
"Exposes uniqueness for Sets."));
constraints.add(
new Constraint("$.properties.shippingAddresses.type", is("array"), "Exposes collection of complex types."));
constraints
.add(new Constraint("$.properties.shippingAddresses.uniqueItems", is(true), "Exposes uniqueness for Sets."));
constraints.add(new Constraint("$.properties.shippingAddresses.items['$ref']", is("#/descriptors/address"),
"References descriptor of complex element type."));