DATAREST-294 - RepositoryRestConfiguration has more fine grained control over media type defaults.

RepositorRestConfiguration now has a ….useHalAsDefaultJsonMediaType() which defaults to true. Configuring this to false allows to still use HAL as default media type in case no Accept header is set or it is set to */*. The flag decides what to return when an Accept header is set to application/json.
This commit is contained in:
Oliver Gierke
2014-04-29 10:09:15 +02:00
parent b69b715a66
commit 7dfdaa5114
3 changed files with 29 additions and 3 deletions

View File

@@ -38,6 +38,7 @@ public class RepositoryRestConfiguration {
private String limitParamName = "size";
private String sortParamName = "sort";
private MediaType defaultMediaType = MediaTypes.HAL_JSON;
private boolean useHalAsDefaultJsonMediaType = true;
private boolean returnBodyOnCreate = false;
private boolean returnBodyOnUpdate = false;
private List<Class<?>> exposeIdsFor = new ArrayList<Class<?>>();
@@ -202,7 +203,7 @@ public class RepositoryRestConfiguration {
/**
* Set the {@link MediaType} to use as a default when none is specified.
*
* @param defaultMediaType Default content type if none has been specified.
* @param defaultMediaType default content type if none has been specified.
* @return {@literal this}
*/
public RepositoryRestConfiguration setDefaultMediaType(MediaType defaultMediaType) {
@@ -210,6 +211,30 @@ public class RepositoryRestConfiguration {
return this;
}
/**
* Returns whether HAL will be served as primary representation in case on {@code application/json} is requested. This
* defaults to {@literal true}. If configured to {@literal false} the legacy Spring Data representation will be
* rendered.
*
* @return
*/
public boolean useHalAsDefaultJsonMediaType() {
return this.useHalAsDefaultJsonMediaType;
}
/**
* Configures whether HAL will be served as primary representation in case on {@code application/json} is requested.
* This defaults to {@literal true}. If configured to {@literal false} the legacy Spring Data representation will be
* rendered.
*
* @param useHalAsDefaultJsonMediaType
* @return
*/
public RepositoryRestConfiguration useHalAsDefaultJsonMediaType(boolean useHalAsDefaultJsonMediaType) {
this.useHalAsDefaultJsonMediaType = useHalAsDefaultJsonMediaType;
return this;
}
/**
* Whether to return a response body after creating an entity.
*

View File

@@ -372,7 +372,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
MediaType.valueOf("application/x-spring-data-compact+json")));
// Configure this mapper to be used if HAL is not the default media type
if (!config().getDefaultMediaType().equals(MediaTypes.HAL_JSON)) {
if (!config().useHalAsDefaultJsonMediaType()) {
mediaTypes.add(MediaType.APPLICATION_JSON);
}
@@ -394,7 +394,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
mediaTypes.add(MediaTypes.HAL_JSON);
// Enable returning HAL if application/json is asked if it's configured to be the default type
if (config().getDefaultMediaType().equals(MediaTypes.HAL_JSON)) {
if (config().useHalAsDefaultJsonMediaType()) {
mediaTypes.add(MediaType.APPLICATION_JSON);
}

View File

@@ -39,6 +39,7 @@ public class LegacyRepresentationConfigIntegrationTests extends AbstractReposito
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultMediaType(MediaType.APPLICATION_JSON);
config.useHalAsDefaultJsonMediaType(false);
}
}