|
8 | 8 | import static io.opentelemetry.instrumentation.spring.webflux.server.AbstractSpringWebFluxServerTest.NESTED_PATH; |
9 | 9 |
|
10 | 10 | import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; |
| 11 | +import java.lang.reflect.Method; |
11 | 12 | import java.net.URI; |
12 | 13 | import java.util.function.Supplier; |
13 | 14 | import org.springframework.http.HttpStatus; |
|
19 | 20 |
|
20 | 21 | @SuppressWarnings("IdentifierName") // method names are snake_case to match endpoints |
21 | 22 | public abstract class ServerTestController { |
| 23 | + |
| 24 | + // Spring 5.x uses setStatusCode(HttpStatus), Spring 6+ uses setStatusCode(HttpStatusCode) |
| 25 | + private static final Method setStatusCodeMethod; |
| 26 | + |
| 27 | + static { |
| 28 | + Method method; |
| 29 | + try { |
| 30 | + // Try Spring 6+ signature first (HttpStatusCode interface) |
| 31 | + Class<?> httpStatusCodeClass = Class.forName("org.springframework.http.HttpStatusCode"); |
| 32 | + method = ServerHttpResponse.class.getMethod("setStatusCode", httpStatusCodeClass); |
| 33 | + } catch (ClassNotFoundException | NoSuchMethodException e) { |
| 34 | + // Fall back to Spring 5.x signature (HttpStatus enum) |
| 35 | + try { |
| 36 | + method = ServerHttpResponse.class.getMethod("setStatusCode", HttpStatus.class); |
| 37 | + } catch (NoSuchMethodException ex) { |
| 38 | + throw new IllegalStateException(ex); |
| 39 | + } |
| 40 | + } |
| 41 | + setStatusCodeMethod = method; |
| 42 | + } |
| 43 | + |
22 | 44 | @GetMapping("/success") |
23 | 45 | public Mono<String> success(ServerHttpResponse response) { |
24 | 46 | ServerEndpoint endpoint = ServerEndpoint.SUCCESS; |
@@ -134,6 +156,11 @@ public Mono<String> nested_path(ServerHttpRequest request, ServerHttpResponse re |
134 | 156 | protected abstract <T> Mono<T> wrapControllerMethod(ServerEndpoint endpoint, Supplier<T> handler); |
135 | 157 |
|
136 | 158 | protected void setStatus(ServerHttpResponse response, ServerEndpoint endpoint) { |
137 | | - response.setStatusCode(HttpStatus.resolve(endpoint.getStatus())); |
| 159 | + HttpStatus status = HttpStatus.resolve(endpoint.getStatus()); |
| 160 | + try { |
| 161 | + setStatusCodeMethod.invoke(response, status); |
| 162 | + } catch (ReflectiveOperationException e) { |
| 163 | + throw new IllegalStateException("Failed to set status code", e); |
| 164 | + } |
138 | 165 | } |
139 | 166 | } |
0 commit comments