Skip to content

Commit bc3f12a

Browse files
committed
Handle Pageable POST params as query maps. Update docs. Fixes gh-753.
1 parent 68c4513 commit bc3f12a

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

docs/src/main/asciidoc/spring-cloud-openfeign.adoc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,15 @@ In the following example, the `CSV` format is used instead of the default `EXPLO
758758
[source,java,indent=0]
759759
----
760760
@FeignClient(name = "demo")
761-
protected interface PageableFeignClient {
761+
protected interface DemoFeignClient {
762762
763763
@CollectionFormat(feign.CollectionFormat.CSV)
764-
@GetMapping(path = "/page")
765-
ResponseEntity performRequest(Pageable page);
764+
@GetMapping(path = "/test")
765+
ResponseEntity performRequest(String test);
766766
767767
}
768768
----
769769

770-
TIP: Set the `CSV` format while sending `Pageable` as a query parameter in order for it to be encoded correctly.
771-
772770
=== Reactive Support
773771
As the https://github.com/OpenFeign/feign[OpenFeign project] does not currently support reactive clients, such as https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.html[Spring WebClient], neither does Spring Cloud OpenFeign.We will add support for it here as soon as it becomes available in the core project.
774772

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringMvcContract.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,7 @@
6060
import org.springframework.core.convert.support.DefaultConversionService;
6161
import org.springframework.core.io.DefaultResourceLoader;
6262
import org.springframework.core.io.ResourceLoader;
63+
import org.springframework.data.domain.Pageable;
6364
import org.springframework.http.InvalidMediaTypeException;
6465
import org.springframework.http.MediaType;
6566
import org.springframework.util.Assert;
@@ -265,6 +266,16 @@ private void checkOne(Method method, Object[] values, String fieldName) {
265266
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
266267
boolean isHttpAnnotation = false;
267268

269+
try {
270+
if (Pageable.class.isAssignableFrom(data.method().getParameterTypes()[paramIndex])) {
271+
data.queryMapIndex(paramIndex);
272+
return false;
273+
}
274+
}
275+
catch (NoClassDefFoundError ignored) {
276+
// Do nothing; added to avoid exceptions if optional dependency not present
277+
}
278+
268279
AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext(data,
269280
paramIndex);
270281
Method method = processedMethods.get(data.configKey());

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@
3737
import org.springframework.cloud.openfeign.CollectionFormat;
3838
import org.springframework.cloud.openfeign.SpringQueryMap;
3939
import org.springframework.core.convert.ConversionService;
40+
import org.springframework.data.domain.Page;
41+
import org.springframework.data.domain.Pageable;
4042
import org.springframework.format.annotation.DateTimeFormat;
4143
import org.springframework.format.annotation.NumberFormat;
4244
import org.springframework.format.number.NumberStyleFormatter;
@@ -625,6 +627,16 @@ void testMultipleCookiesAnnotation() throws NoSuchMethodException {
625627
.isEqualTo("cookie1={cookie1}; cookie2={cookie2}");
626628
}
627629

630+
@Test
631+
void shouldNotFailWhenBothPageableAndRequestBodyParamsInPostRequest() {
632+
List<MethodMetadata> data = contract.parseAndValidateMetadata(TestTemplate_PageablePost.class);
633+
634+
assertThat(data.get(0).queryMapIndex().intValue()).isEqualTo(0);
635+
assertThat(data.get(0).bodyIndex().intValue()).isEqualTo(1);
636+
assertThat(data.get(1).queryMapIndex().intValue()).isEqualTo(1);
637+
assertThat(data.get(1).bodyIndex().intValue()).isEqualTo(0);
638+
}
639+
628640
private ConversionService getConversionService() {
629641
FormattingConversionServiceFactoryBean conversionServiceFactoryBean = new FormattingConversionServiceFactoryBean();
630642
conversionServiceFactoryBean.afterPropertiesSet();
@@ -826,6 +838,16 @@ public interface TestTemplate_NumberFormatParameter {
826838

827839
}
828840

841+
public interface TestTemplate_PageablePost {
842+
843+
@PostMapping
844+
Page<String> getPage(Pageable pageable, @RequestBody String body);
845+
846+
@PostMapping
847+
Page<String> getPage(@RequestBody String body, Pageable pageable);
848+
849+
}
850+
829851
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
830852
public class TestObject {
831853

0 commit comments

Comments
 (0)