Skip to content

Commit d8fe4ac

Browse files
Wynn TeoWynn Teo
authored andcommitted
BAEL-8657
1 parent 8f8cf5c commit d8fe4ac

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.cloud.openfeign.getbody;
2+
3+
import org.springframework.cloud.openfeign.FeignClient;
4+
import org.springframework.cloud.openfeign.SpringQueryMap;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
8+
@FeignClient(name = "sampleClient", url = "http://localhost:8080")
9+
public interface GetBodyFeignClient {
10+
11+
@GetMapping("/api/search")
12+
String search(@RequestBody SearchRequest searchRequest);
13+
14+
@GetMapping("/api/search")
15+
String searchWithSpringQueryMap(@SpringQueryMap SearchRequest searchRequest);
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.cloud.openfeign.getbody;
2+
3+
public class SearchRequest {
4+
private String keyword;
5+
private String category;
6+
7+
public SearchRequest() {
8+
9+
}
10+
11+
public SearchRequest(String keyword, String category) {
12+
this.keyword = keyword;
13+
this.category = category;
14+
}
15+
16+
public String getKeyword() {
17+
return keyword;
18+
}
19+
20+
public void setKeyword(String keyword) {
21+
this.keyword = keyword;
22+
}
23+
24+
public String getCategory() {
25+
return category;
26+
}
27+
28+
public void setCategory(String category) {
29+
this.category = category;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.cloud.openfeign.getbody;
2+
import com.github.tomakehurst.wiremock.WireMockServer;
3+
import com.github.tomakehurst.wiremock.client.WireMock;
4+
5+
import org.junit.jupiter.api.*;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
9+
@SpringBootTest
10+
public class GetBodyFeignClientIntegrationTest {
11+
12+
private static WireMockServer wireMockServer;
13+
14+
@Autowired
15+
private GetBodyFeignClient getBodyFeignClient;
16+
17+
@BeforeAll
18+
static void setupWireMock() {
19+
wireMockServer = new WireMockServer(8080);
20+
wireMockServer.start();
21+
22+
WireMock.configureFor("localhost", 8080);
23+
24+
// Stub endpoint
25+
WireMock.stubFor(WireMock.get(WireMock.urlMatching("/api/search.*"))
26+
.willReturn(WireMock.aResponse()
27+
.withStatus(200)
28+
.withBody("GET request received")));
29+
30+
}
31+
32+
@AfterAll
33+
static void stopWireMock() {
34+
wireMockServer.stop();
35+
}
36+
37+
@Test
38+
void givenRequestBody_whenCallGetHTTPMethod_returnException() {
39+
SearchRequest request = new SearchRequest();
40+
request.setKeyword("spring");
41+
request.setCategory("tutorial");
42+
43+
Assertions.assertThrows(feign.FeignException.class, () -> {
44+
getBodyFeignClient.search(request);
45+
});
46+
}
47+
48+
@Test
49+
void givenRequestBody_whenUsingSpringQueryMap_thenRequestSucceeds() {
50+
SearchRequest request = new SearchRequest();
51+
request.setKeyword("spring");
52+
request.setCategory("tutorial");
53+
54+
getBodyFeignClient.searchWithSpringQueryMap(request);
55+
56+
WireMock.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/api/search?keyword=spring&category=tutorial")));
57+
}
58+
}

0 commit comments

Comments
 (0)