Respect SpringDataWebProperties where possible (#583)
This commit is contained in:
@@ -71,6 +71,7 @@ import static feign.form.ContentType.MULTIPART;
|
||||
* @author Jonatan Ivanov
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Hyeonmin Park
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class FeignClientsConfiguration {
|
||||
@@ -130,7 +131,13 @@ public class FeignClientsConfiguration {
|
||||
@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
|
||||
@ConditionalOnMissingBean
|
||||
public QueryMapEncoder feignQueryMapEncoderPageable() {
|
||||
return new PageableSpringQueryMapEncoder();
|
||||
PageableSpringQueryMapEncoder queryMapEncoder = new PageableSpringQueryMapEncoder();
|
||||
if (springDataWebProperties != null) {
|
||||
queryMapEncoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter());
|
||||
queryMapEncoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter());
|
||||
queryMapEncoder.setSortParameter(springDataWebProperties.getSort().getSortParameter());
|
||||
}
|
||||
return queryMapEncoder;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.data.domain.Sort;
|
||||
* Provides support for encoding spring Pageable via composition.
|
||||
*
|
||||
* @author Pascal Büttiker
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
public class PageableSpringEncoder implements Encoder {
|
||||
|
||||
@@ -82,8 +83,8 @@ public class PageableSpringEncoder implements Encoder {
|
||||
Pageable pageable = (Pageable) object;
|
||||
|
||||
if (pageable.isPaged()) {
|
||||
template.query(pageParameter, pageable.getPageNumber() + "");
|
||||
template.query(sizeParameter, pageable.getPageSize() + "");
|
||||
template.query(pageParameter, String.valueOf(pageable.getPageNumber()));
|
||||
template.query(sizeParameter, String.valueOf(pageable.getPageSize()));
|
||||
}
|
||||
|
||||
if (pageable.getSort() != null) {
|
||||
|
||||
@@ -31,10 +31,38 @@ import org.springframework.data.domain.Sort;
|
||||
* {@link org.springframework.cloud.openfeign.SpringQueryMap}.
|
||||
*
|
||||
* @author Hyeonmin Park
|
||||
* @author Yanming Zhou
|
||||
* @since 2.2.8
|
||||
*/
|
||||
public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder {
|
||||
|
||||
/**
|
||||
* Page index parameter name.
|
||||
*/
|
||||
private String pageParameter = "page";
|
||||
|
||||
/**
|
||||
* Page size parameter name.
|
||||
*/
|
||||
private String sizeParameter = "size";
|
||||
|
||||
/**
|
||||
* Sort parameter name.
|
||||
*/
|
||||
private String sortParameter = "sort";
|
||||
|
||||
public void setPageParameter(String pageParameter) {
|
||||
this.pageParameter = pageParameter;
|
||||
}
|
||||
|
||||
public void setSizeParameter(String sizeParameter) {
|
||||
this.sizeParameter = sizeParameter;
|
||||
}
|
||||
|
||||
public void setSortParameter(String sortParameter) {
|
||||
this.sortParameter = sortParameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> encode(Object object) {
|
||||
if (supports(object)) {
|
||||
@@ -44,8 +72,8 @@ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder {
|
||||
Pageable pageable = (Pageable) object;
|
||||
|
||||
if (pageable.isPaged()) {
|
||||
queryMap.put("page", pageable.getPageNumber());
|
||||
queryMap.put("size", pageable.getPageSize());
|
||||
queryMap.put(pageParameter, pageable.getPageNumber());
|
||||
queryMap.put(sizeParameter, pageable.getPageSize());
|
||||
}
|
||||
|
||||
if (pageable.getSort() != null) {
|
||||
@@ -69,7 +97,7 @@ public class PageableSpringQueryMapEncoder extends BeanQueryMapEncoder {
|
||||
sortQueries.add(order.getProperty() + "%2C" + order.getDirection());
|
||||
}
|
||||
if (!sortQueries.isEmpty()) {
|
||||
queryMap.put("sort", sortQueries);
|
||||
queryMap.put(sortParameter, sortQueries);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
* Tests the pagination encoding and sorting.
|
||||
*
|
||||
* @author Charlie Mordant.
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT,
|
||||
@@ -55,6 +56,18 @@ public class PageableEncoderTests {
|
||||
@Autowired
|
||||
private FeignContext context;
|
||||
|
||||
protected String getPageParameter() {
|
||||
return "page";
|
||||
}
|
||||
|
||||
protected String getSizeParameter() {
|
||||
return "size";
|
||||
}
|
||||
|
||||
protected String getSortParameter() {
|
||||
return "sort";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginationAndSortingRequest() {
|
||||
Encoder encoder = this.context.getInstance("foo", Encoder.class);
|
||||
@@ -65,11 +78,11 @@ public class PageableEncoderTests {
|
||||
// Request queries shall contain three entries
|
||||
assertThat(request.queries()).hasSize(3);
|
||||
// Request page shall contain page
|
||||
assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
|
||||
assertThat(request.queries().get(getPageParameter())).contains(String.valueOf(PAGE));
|
||||
// Request size shall contain size
|
||||
assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
|
||||
assertThat(request.queries().get(getSizeParameter())).contains(String.valueOf(SIZE));
|
||||
// Request sort size shall contain sort entries
|
||||
assertThat(request.queries().get("sort")).hasSize(2);
|
||||
assertThat(request.queries().get(getSortParameter())).hasSize(2);
|
||||
}
|
||||
|
||||
private Pageable createPageAndSortRequest() {
|
||||
@@ -84,11 +97,11 @@ public class PageableEncoderTests {
|
||||
encoder.encode(createPageAndRequest(), null, request);
|
||||
assertThat(request.queries().size()).isEqualTo(2);
|
||||
// Request page shall contain page
|
||||
assertThat(request.queries().get("page")).contains(String.valueOf(PAGE));
|
||||
assertThat(request.queries().get(getPageParameter())).contains(String.valueOf(PAGE));
|
||||
// Request size shall contain size
|
||||
assertThat(request.queries().get("size")).contains(String.valueOf(SIZE));
|
||||
assertThat(request.queries().get(getSizeParameter())).contains(String.valueOf(SIZE));
|
||||
// Request sort size shall contain sort entries
|
||||
assertThat(request.queries()).doesNotContainKey("sort");
|
||||
assertThat(request.queries()).doesNotContainKey(getSortParameter());
|
||||
}
|
||||
|
||||
private Pageable createPageAndRequest() {
|
||||
@@ -105,7 +118,7 @@ public class PageableEncoderTests {
|
||||
// Request queries shall contain three entries
|
||||
assertThat(request.queries().size()).isEqualTo(1);
|
||||
// Request sort size shall contain sort entries
|
||||
assertThat(request.queries().get("sort")).hasSize(2);
|
||||
assertThat(request.queries().get(getSortParameter())).hasSize(2);
|
||||
}
|
||||
|
||||
private Sort createSort() {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2013-2020 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
|
||||
*
|
||||
* https://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.cloud.openfeign.support;
|
||||
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Tests the pagination encoding and sorting.
|
||||
*
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@EnableConfigurationProperties(SpringDataWebProperties.class)
|
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT,
|
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false",
|
||||
"spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize",
|
||||
"spring.data.web.sort.sortParameter=orderBy" })
|
||||
public class PageableEncoderWithSpringDataWebTests extends PageableEncoderTests {
|
||||
|
||||
@Override
|
||||
protected String getPageParameter() {
|
||||
return "pageNo";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSizeParameter() {
|
||||
return "pageSize";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSortParameter() {
|
||||
return "orderBy";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 2013-2020 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
|
||||
*
|
||||
* https://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.cloud.openfeign.support;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import feign.QueryMapEncoder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.openfeign.FeignContext;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Tests the pagination encoding and sorting.
|
||||
*
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT,
|
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false" })
|
||||
@DirtiesContext
|
||||
public class PageableSpringQueryMapEncoderTests {
|
||||
|
||||
public static final int PAGE = 1;
|
||||
|
||||
public static final int SIZE = 10;
|
||||
|
||||
public static final String SORT_2 = "sort2";
|
||||
|
||||
public static final String SORT_1 = "sort1";
|
||||
|
||||
@Autowired
|
||||
private FeignContext context;
|
||||
|
||||
protected String getPageParameter() {
|
||||
return "page";
|
||||
}
|
||||
|
||||
protected String getSizeParameter() {
|
||||
return "size";
|
||||
}
|
||||
|
||||
protected String getSortParameter() {
|
||||
return "sort";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginationAndSortingRequest() {
|
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class);
|
||||
assertThat(encoder).isNotNull();
|
||||
|
||||
Map<String, Object> map = encoder.encode(createPageAndSortRequest());
|
||||
assertThat(map).hasSize(3);
|
||||
assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE);
|
||||
assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE);
|
||||
assertThat((List<?>) map.get(getSortParameter())).hasSize(2);
|
||||
}
|
||||
|
||||
private Pageable createPageAndSortRequest() {
|
||||
return PageRequest.of(PAGE, SIZE, Sort.Direction.ASC, SORT_1, SORT_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginationRequest() {
|
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class);
|
||||
assertThat(encoder).isNotNull();
|
||||
|
||||
Map<String, Object> map = encoder.encode(createPageAndRequest());
|
||||
assertThat(map).hasSize(2);
|
||||
assertThat((Integer) map.get(getPageParameter())).isEqualTo(PAGE);
|
||||
assertThat((Integer) map.get(getSizeParameter())).isEqualTo(SIZE);
|
||||
assertThat(map).doesNotContainKey(getSortParameter());
|
||||
}
|
||||
|
||||
private Pageable createPageAndRequest() {
|
||||
return PageRequest.of(PAGE, SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortingRequest() {
|
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class);
|
||||
assertThat(encoder).isNotNull();
|
||||
|
||||
Map<String, Object> map = encoder.encode(createSort());
|
||||
assertThat(map).hasSize(1);
|
||||
assertThat((List<?>) map.get(getSortParameter())).hasSize(2);
|
||||
}
|
||||
|
||||
private Sort createSort() {
|
||||
return Sort.by(SORT_1, SORT_2).ascending();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnpagedRequest() {
|
||||
QueryMapEncoder encoder = this.context.getInstance("foo", QueryMapEncoder.class);
|
||||
assertThat(encoder).isNotNull();
|
||||
|
||||
Map<String, Object> map = encoder.encode(Pageable.unpaged());
|
||||
assertThat(map).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2013-2020 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
|
||||
*
|
||||
* https://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.cloud.openfeign.support;
|
||||
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Tests the pagination encoding and sorting.
|
||||
*
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@EnableConfigurationProperties(SpringDataWebProperties.class)
|
||||
@SpringBootTest(classes = SpringEncoderTests.Application.class, webEnvironment = RANDOM_PORT,
|
||||
value = { "spring.application.name=springencodertest", "spring.jmx.enabled=false",
|
||||
"spring.data.web.pageable.pageParameter=pageNo", "spring.data.web.pageable.sizeParameter=pageSize",
|
||||
"spring.data.web.sort.sortParameter=orderBy" })
|
||||
public class PageableSpringQueryMapEncoderWithSpringDataWebTests extends PageableSpringQueryMapEncoderTests {
|
||||
|
||||
@Override
|
||||
protected String getPageParameter() {
|
||||
return "pageNo";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSizeParameter() {
|
||||
return "pageSize";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSortParameter() {
|
||||
return "orderBy";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user