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