|
1 | 1 | package io.sentry.samples.spring.boot4; |
2 | 2 |
|
3 | 3 | import io.sentry.reactor.SentryReactorUtils; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.UncheckedIOException; |
| 6 | +import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.Request; |
| 8 | +import okhttp3.Response; |
4 | 9 | import org.springframework.web.bind.annotation.GetMapping; |
5 | 10 | import org.springframework.web.bind.annotation.PathVariable; |
6 | 11 | import org.springframework.web.bind.annotation.RestController; |
|
10 | 15 | import reactor.core.publisher.Hooks; |
11 | 16 | import reactor.core.publisher.Mono; |
12 | 17 | import reactor.core.scheduler.Schedulers; |
| 18 | +import tools.jackson.databind.ObjectMapper; |
13 | 19 |
|
14 | 20 | @RestController |
15 | 21 | public class TodoController { |
16 | 22 | private final RestTemplate restTemplate; |
17 | 23 | private final WebClient webClient; |
18 | 24 | private final RestClient restClient; |
| 25 | + private final OkHttpClient okHttpClient; |
| 26 | + private final ObjectMapper objectMapper; |
19 | 27 |
|
20 | | - public TodoController(RestTemplate restTemplate, WebClient webClient, RestClient restClient) { |
| 28 | + public TodoController( |
| 29 | + RestTemplate restTemplate, |
| 30 | + WebClient webClient, |
| 31 | + RestClient restClient, |
| 32 | + OkHttpClient okHttpClient, |
| 33 | + ObjectMapper objectMapper) { |
21 | 34 | this.restTemplate = restTemplate; |
22 | 35 | this.webClient = webClient; |
23 | 36 | this.restClient = restClient; |
| 37 | + this.okHttpClient = okHttpClient; |
| 38 | + this.objectMapper = objectMapper; |
24 | 39 | } |
25 | 40 |
|
26 | 41 | @GetMapping("/todo/{id}") |
@@ -54,4 +69,15 @@ Todo todoRestClient(@PathVariable Long id) { |
54 | 69 | .retrieve() |
55 | 70 | .body(Todo.class); |
56 | 71 | } |
| 72 | + |
| 73 | + @GetMapping("/todo-okhttp/{id}") |
| 74 | + Todo todoOkHttp(@PathVariable Long id) { |
| 75 | + final Request request = |
| 76 | + new Request.Builder().url("https://jsonplaceholder.typicode.com/todos/" + id).build(); |
| 77 | + try (Response response = okHttpClient.newCall(request).execute()) { |
| 78 | + return objectMapper.readValue(response.body().byteStream(), Todo.class); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new UncheckedIOException(e); |
| 81 | + } |
| 82 | + } |
57 | 83 | } |
0 commit comments