Skip to content

Commit 565b6cd

Browse files
committed
add feign client
1 parent f7b325e commit 565b6cd

6 files changed

Lines changed: 58 additions & 15 deletions

File tree

backend/src/main/java/com/booking/backend/controller/PersonController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public PersonDto getPersonById(@RequestParam Long id) {
2020
@PostMapping("/person")
2121
public void addNewBooking(@RequestBody PersonDto personDto) {
2222
personService.updatePerson(personDto);
23+
2324
}
2425
}

bot/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@
4141
<groupId>org.projectlombok</groupId>
4242
<artifactId>lombok</artifactId>
4343
</dependency>
44-
</dependencies>
44+
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-starter-openfeign</artifactId>
48+
<version>3.1.1</version>
49+
</dependency>
50+
51+
</dependencies>
4552

4653
<build>
4754
<plugins>

bot/src/main/java/com/booking/bot/BotApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.openfeign.EnableFeignClients;
56

67
@SpringBootApplication
8+
@EnableFeignClients
79
public class BotApplication {
810

911
public static void main(String[] args) {

bot/src/main/java/com/booking/bot/adapter/BookingAdapter.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.booking.bot.adapter;
22

3+
import com.booking.bot.client.BookingClient;
4+
import com.booking.bot.client.PersonClient;
35
import com.booking.bot.dto.OrganizationDto;
46
import com.booking.bot.dto.PersonDto;
7+
import lombok.RequiredArgsConstructor;
58
import org.springframework.http.HttpHeaders;
69
import org.springframework.http.MediaType;
710
import org.springframework.stereotype.Component;
@@ -14,28 +17,20 @@
1417
import java.util.stream.Collectors;
1518

1619
@Component
20+
@RequiredArgsConstructor
1721
public class BookingAdapter {
1822

19-
private final WebClient client;
20-
21-
public BookingAdapter() {
22-
this.client = WebClient.create("http://localhost:8080");
23-
}
23+
private final BookingClient bookingClient;
24+
private final PersonClient personClient;
2425

2526
public void addPerson(PersonDto personDto, String uri) {
26-
client
27-
.post()
28-
.uri(uri)
29-
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
30-
.body(Mono.just(personDto), PersonDto.class)
31-
.retrieve()
32-
.bodyToMono(String.class)
33-
.share()
34-
.block();
27+
personClient.addNewBooking(personDto);
28+
// todo: обрабатывать ошибки
3529
}
3630

3731

3832
public List<OrganizationDto> getOrganization(String uri, String organizationName) {
33+
3934
return Arrays.asList(Objects.requireNonNull(client.get().uri(uri, organizationName)
4035
.retrieve().bodyToMono(OrganizationDto[].class).share().block()));
4136
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.booking.bot.client;
2+
3+
import com.booking.bot.dto.BookingDto;
4+
import org.springframework.cloud.openfeign.FeignClient;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.List;
8+
9+
@FeignClient(url="localhost:8080")
10+
public interface BookingClient {
11+
@GetMapping("/booking")
12+
@RequestMapping(value = "/booking", params = {"pageNo","pageSize","sortBy"})
13+
List<BookingDto> getAll(@RequestParam Integer pageNo,
14+
@RequestParam Integer pageSize,
15+
@RequestParam String sortBy);
16+
17+
@PostMapping("/bookings")
18+
void addNewBooking(@RequestBody BookingDto bookingDto);
19+
20+
}
21+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.booking.bot.client;
2+
3+
import com.booking.bot.dto.PersonDto;
4+
import org.springframework.cloud.openfeign.FeignClient;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
@FeignClient(url="localhost:8080")
8+
public interface PersonClient {
9+
10+
@GetMapping("/person")
11+
@RequestMapping(value = "/person", params = "id")
12+
PersonDto getPersonById(@RequestParam Long id);
13+
14+
@PostMapping("/person")
15+
void addNewBooking(@RequestBody PersonDto personDto);
16+
}
17+

0 commit comments

Comments
 (0)