001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.data.web; 018 019import org.springframework.boot.autoconfigure.AutoConfigureAfter; 020import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 021import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 022import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; 025import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; 026import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable; 027import org.springframework.boot.context.properties.EnableConfigurationProperties; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.data.domain.PageRequest; 031import org.springframework.data.web.PageableHandlerMethodArgumentResolver; 032import org.springframework.data.web.config.EnableSpringDataWebSupport; 033import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer; 034import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer; 035import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 036 037/** 038 * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's web support. 039 * <p> 040 * When in effect, the auto-configuration is the equivalent of enabling Spring Data's web 041 * support through the {@link EnableSpringDataWebSupport} annotation. 042 * 043 * @author Andy Wilkinson 044 * @author Vedran Pavic 045 * @since 1.2.0 046 */ 047@Configuration 048@EnableSpringDataWebSupport 049@ConditionalOnWebApplication(type = Type.SERVLET) 050@ConditionalOnClass({ PageableHandlerMethodArgumentResolver.class, 051 WebMvcConfigurer.class }) 052@ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class) 053@EnableConfigurationProperties(SpringDataWebProperties.class) 054@AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class) 055public class SpringDataWebAutoConfiguration { 056 057 private final SpringDataWebProperties properties; 058 059 public SpringDataWebAutoConfiguration(SpringDataWebProperties properties) { 060 this.properties = properties; 061 } 062 063 @Bean 064 @ConditionalOnMissingBean 065 public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() { 066 return (resolver) -> { 067 Pageable pageable = this.properties.getPageable(); 068 resolver.setPageParameterName(pageable.getPageParameter()); 069 resolver.setSizeParameterName(pageable.getSizeParameter()); 070 resolver.setOneIndexedParameters(pageable.isOneIndexedParameters()); 071 resolver.setPrefix(pageable.getPrefix()); 072 resolver.setQualifierDelimiter(pageable.getQualifierDelimiter()); 073 resolver.setFallbackPageable( 074 PageRequest.of(0, pageable.getDefaultPageSize())); 075 resolver.setMaxPageSize(pageable.getMaxPageSize()); 076 }; 077 } 078 079 @Bean 080 @ConditionalOnMissingBean 081 public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() { 082 return (resolver) -> resolver 083 .setSortParameter(this.properties.getSort().getSortParameter()); 084 } 085 086}