Improve pageable parameter names. (#181)
Improve pageable parameter names by reusing spring boot properties.
This commit is contained in:
@@ -36,6 +36,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.support.PageJacksonModule;
|
||||
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
||||
@@ -69,6 +70,9 @@ public class FeignClientsConfiguration {
|
||||
@Autowired(required = false)
|
||||
private Logger logger;
|
||||
|
||||
@Autowired(required = false)
|
||||
private SpringDataWebProperties springDataWebProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Decoder feignDecoder() {
|
||||
@@ -87,7 +91,13 @@ public class FeignClientsConfiguration {
|
||||
@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
|
||||
@ConditionalOnMissingBean
|
||||
public Encoder feignEncoderPageable() {
|
||||
return new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
|
||||
PageableSpringEncoder encoder = new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
|
||||
if (springDataWebProperties != null) {
|
||||
encoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter());
|
||||
encoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter());
|
||||
encoder.setSortParameter(springDataWebProperties.getSort().getSortParameter());
|
||||
}
|
||||
return encoder;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -37,6 +37,21 @@ public class PageableSpringEncoder implements Encoder {
|
||||
|
||||
private final Encoder delegate;
|
||||
|
||||
/**
|
||||
* Page index parameter name.
|
||||
*/
|
||||
private String pageParameter = "page";
|
||||
|
||||
/**
|
||||
* Page size parameter name.
|
||||
*/
|
||||
private String sizeParameter = "size";
|
||||
|
||||
/**
|
||||
* Sort parameter name.
|
||||
*/
|
||||
private String sortParameter = "sort";
|
||||
|
||||
/**
|
||||
* Creates a new PageableSpringEncoder with the given delegate for fallback. If no
|
||||
* delegate is provided and this encoder cant handle the request, an EncodeException
|
||||
@@ -47,6 +62,18 @@ public class PageableSpringEncoder implements Encoder {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
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 void encode(Object object, Type bodyType, RequestTemplate template)
|
||||
throws EncodeException {
|
||||
@@ -54,8 +81,8 @@ public class PageableSpringEncoder implements Encoder {
|
||||
if (supports(object)) {
|
||||
if (object instanceof Pageable) {
|
||||
Pageable pageable = (Pageable) object;
|
||||
template.query("page", pageable.getPageNumber() + "");
|
||||
template.query("size", pageable.getPageSize() + "");
|
||||
template.query(pageParameter, pageable.getPageNumber() + "");
|
||||
template.query(sizeParameter, pageable.getPageSize() + "");
|
||||
if (pageable.getSort() != null) {
|
||||
applySort(template, pageable.getSort());
|
||||
}
|
||||
@@ -82,11 +109,17 @@ public class PageableSpringEncoder implements Encoder {
|
||||
Collection<String> existingSorts = template.queries().get("sort");
|
||||
List<String> sortQueries = existingSorts != null ? new ArrayList<>(existingSorts)
|
||||
: new ArrayList<>();
|
||||
if (!sortParameter.equals("sort")) {
|
||||
existingSorts = template.queries().get(sortParameter);
|
||||
if (existingSorts != null) {
|
||||
sortQueries.addAll(existingSorts);
|
||||
}
|
||||
}
|
||||
for (Sort.Order order : sort) {
|
||||
sortQueries.add(order.getProperty() + "," + order.getDirection());
|
||||
}
|
||||
if (!sortQueries.isEmpty()) {
|
||||
template.query("sort", sortQueries);
|
||||
template.query(sortParameter, sortQueries);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user