Skip to content

Commit 04fd262

Browse files
[Java] Add API for testing downstream error code (#436)
## Changes Adding a new API `/status/{code}?ip=...` that calls the downstream service similar to `/remote-service` and specifies the code it wants the remote service to respond with. The remote service now accepted `/status/{code}` and responds with status code `{code}`. The main service will respond with OK and the trace ID in any case. ## Testing Built and ran the applications locally, confirming functionality of the new APIs. ## Risks New API does not change any functionality of old ones. Could cause build issues, but we use other imports from the same dependencies and the same logic in `/remote-service` so there is little to no risk. ## Rollback procedure Revert commit and rerun sample app deployment script. #### By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c7bbd26 commit 04fd262

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

sample-apps/java/springboot-main-service/src/main/java/com/amazon/sampleapp/FrontendServiceController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import org.springframework.beans.factory.annotation.Autowired;
3939
import org.springframework.context.annotation.Bean;
4040
import org.springframework.stereotype.Controller;
41+
import org.springframework.http.ResponseEntity;
4142
import org.springframework.web.bind.annotation.GetMapping;
43+
import org.springframework.web.bind.annotation.PathVariable;
4244
import org.springframework.web.bind.annotation.RequestParam;
4345
import org.springframework.web.bind.annotation.ResponseBody;
4446
import software.amazon.awssdk.services.s3.S3Client;
@@ -164,6 +166,20 @@ public String mysql() {
164166
return getXrayTraceId();
165167
}
166168

169+
@GetMapping("/status/{code}")
170+
@ResponseBody
171+
public ResponseEntity<String> status(@PathVariable int code, @RequestParam(name = "ip", defaultValue = "localhost") String ip) {
172+
ip = ip.replace("/", "");
173+
try {
174+
HttpGet request = new HttpGet("http://" + ip + ":8081/status/" + code);
175+
httpClient.execute(request).close();
176+
} catch (Exception e) {
177+
// Ignore exception
178+
}
179+
logger.info("Service A requested status code {} from Service B", code);
180+
return ResponseEntity.ok(getXrayTraceId());
181+
}
182+
167183
// get x-ray trace id
168184
private String getXrayTraceId() {
169185
String traceId = Span.current().getSpanContext().getTraceId();

sample-apps/java/springboot-remote-service/src/main/java/com/amazon/sampleapp/RemoteServiceController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
package com.amazon.sampleapp;
1717

18+
import org.springframework.http.ResponseEntity;
1819
import org.springframework.stereotype.Controller;
1920
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PathVariable;
2022
import org.springframework.web.bind.annotation.ResponseBody;
2123

2224
@Controller
@@ -27,4 +29,9 @@ public class RemoteServiceController {
2729
public String healthcheck() {
2830
return "Remote service healthcheck";
2931
}
32+
33+
@GetMapping("/status/{code}")
34+
public ResponseEntity<String> status(@PathVariable int code) {
35+
return ResponseEntity.status(code).build();
36+
}
3037
}

0 commit comments

Comments
 (0)