diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 5318ec001..866014a04 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -19,11 +19,13 @@ import lombok.RequiredArgsConstructor; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -41,6 +43,7 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; +import org.springframework.util.MimeType; import org.springframework.util.StringUtils; import org.springframework.util.StringValueResolver; import org.springframework.web.bind.annotation.CrossOrigin; @@ -60,6 +63,7 @@ * @author Jon Brisbin * @author Oliver Gierke * @author Mark Paluch + * @author Petar Tahchiev */ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { @@ -69,6 +73,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { private RepositoryCorsConfigurationAccessor corsConfigurationAccessor; private JpaHelper jpaHelper; + private Collection supportedMediaTypes; /** * Creates a new {@link RepositoryRestHandlerMapping} for the given {@link ResourceMappings} and @@ -203,6 +208,9 @@ protected ProducesRequestCondition customize(ProducesRequestCondition condition) HashSet mediaTypes = new LinkedHashSet(); mediaTypes.add(configuration.getDefaultMediaType().toString()); mediaTypes.add(MediaType.APPLICATION_JSON_VALUE); + if (supportedMediaTypes != null) { + mediaTypes.addAll(supportedMediaTypes.stream().map(MimeType::toString).collect(Collectors.toList())); + } return new ProducesRequestCondition(mediaTypes.toArray(new String[mediaTypes.size()])); } @@ -235,6 +243,10 @@ private static String getRepositoryBasePath(String repositoryLookupPath) { return secondSlashIndex == -1 ? repositoryLookupPath : repositoryLookupPath.substring(0, secondSlashIndex); } + public void setSupportedMediaTypes(Collection supportedMediaTypes) { + this.supportedMediaTypes = supportedMediaTypes; + } + /** * No-op {@link StringValueResolver} that returns the given {@link String} value as is. * diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 5be19566b..7c1b5342e 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanCreationException; @@ -173,6 +174,7 @@ * @author Jon Brisbin * @author Greg Turnquist * @author Mark Paluch + * @author Petar Tahchiev */ @Configuration @EnableHypermediaSupport(type = HypermediaType.HAL) @@ -626,6 +628,7 @@ public DelegatingHandlerMapping restHandlerMapping() { repositoryMapping.setJpaHelper(jpaHelper()); repositoryMapping.setApplicationContext(applicationContext); repositoryMapping.setCorsConfigurations(corsConfigurations); + repositoryMapping.setSupportedMediaTypes(getSupportedMediaTypes()); repositoryMapping.afterPropertiesSet(); BasePathAwareHandlerMapping basePathMapping = new BasePathAwareHandlerMapping(repositoryRestConfiguration()); @@ -640,6 +643,16 @@ public DelegatingHandlerMapping restHandlerMapping() { return new DelegatingHandlerMapping(mappings); } + private Collection getSupportedMediaTypes() { + + List result = new ArrayList<>(); + for(HttpMessageConverter httpMessageConverter : defaultMessageConverters()) { + result.addAll(httpMessageConverter.getSupportedMediaTypes()); + } + + return result; + } + @Bean public RepositoryResourceMappings resourceMappings() { return new RepositoryResourceMappings(repositories(), persistentEntities(), repositoryRestConfiguration());