如何在 spring 启动时从 Rest Template 捕获错误响应?
How to capture error responses from Rest Template in spring boot?
我有 2 springboot
REST APIs REST-A 和 REST-B。 REST-B 正在与 mongodb
交互以进行 CRUD 操作。 REST-A 出于不同原因调用 REST-B 端点。
REST-B 中的控制器(客户 API)
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping(value = "/customers/{id}")
public ResponseEntity<Customer> getCustomerByExternalReferenceId(@PathVariable(value = "id") String id)
throws ResourceNotFoundException {
System.out.println("Customer id received :: " + id);
Customer customer = customerRepository.findByExternalCustomerReferenceId(id)
.orElseThrow(() -> new ResourceNotFoundException("Customer not found for this id :: " + id));
return ResponseEntity.ok().body(customer);
}
}
如果我从邮递员处调用此端点,无论是在数据库中找到客户还是在数据库中找不到客户,都可以正常工作。
现在,如果我尝试从 REST-A 调用相同的端点,并且如果在数据库中找到客户,我可以获得响应。
String url = "http://localhost:8086/customer-api/customers/{id}";
String extCustRefId =
setupRequest.getPayload().getCustomer().getCustomerReferenceId();
// URI (URL) parameters
Map<String, String> urlParams = new HashMap<>();
urlParams.put("id", extCustRefId); // here I tried with id that exists in DB and getting 200 ok response
HttpHeaders headers = new HttpHeaders();
headers.set("X-GP-Request-Id", "abc-xyz-123");
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
headers.set("Content-Length", "65");
String searchurl = UriComponentsBuilder.fromUriString(url).buildAndExpand(urlParams).toString();
System.out.println(searchurl);
HttpEntity request = new HttpEntity(headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> response = restTemplate.exchange(
searchurl,
HttpMethod.GET,
request,
String.class
);
} catch (Exception e) {
e.printStackTrace();
}
但是如果没有从 REST-B 中找到客户(客户 API),那么我会得到
http://localhost:8086/customer-api/customers/customer-528f2331-d0c8-46f6-88c2-7445ee6f4821
Customer id received :: customer-528f2331-d0c8-46f6-88c2-7445ee6f4821
org.springframework.web.client.HttpClientErrorException: 404 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
如何从一个 springboot
应用程序调用休息端点到另一个应用程序并正确处理响应?
您可以从HttpClientErrorException
获取响应正文如下:
try {
ResponseEntity<String> response = restTemplate.exchange(
searchurl,
HttpMethod.GET,
request,
String.class
);
} catch (HttpClientErrorException e) {
String errorResponseBody = e.getResponseBodyAsString();
e.printStackTrace();
}
然后您可以使用 Jackson ObjectMapper
将字符串映射到 Java 对象。
我有 2 springboot
REST APIs REST-A 和 REST-B。 REST-B 正在与 mongodb
交互以进行 CRUD 操作。 REST-A 出于不同原因调用 REST-B 端点。
REST-B 中的控制器(客户 API)
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping(value = "/customers/{id}")
public ResponseEntity<Customer> getCustomerByExternalReferenceId(@PathVariable(value = "id") String id)
throws ResourceNotFoundException {
System.out.println("Customer id received :: " + id);
Customer customer = customerRepository.findByExternalCustomerReferenceId(id)
.orElseThrow(() -> new ResourceNotFoundException("Customer not found for this id :: " + id));
return ResponseEntity.ok().body(customer);
}
}
如果我从邮递员处调用此端点,无论是在数据库中找到客户还是在数据库中找不到客户,都可以正常工作。
现在,如果我尝试从 REST-A 调用相同的端点,并且如果在数据库中找到客户,我可以获得响应。
String url = "http://localhost:8086/customer-api/customers/{id}";
String extCustRefId =
setupRequest.getPayload().getCustomer().getCustomerReferenceId();
// URI (URL) parameters
Map<String, String> urlParams = new HashMap<>();
urlParams.put("id", extCustRefId); // here I tried with id that exists in DB and getting 200 ok response
HttpHeaders headers = new HttpHeaders();
headers.set("X-GP-Request-Id", "abc-xyz-123");
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
headers.set("Content-Length", "65");
String searchurl = UriComponentsBuilder.fromUriString(url).buildAndExpand(urlParams).toString();
System.out.println(searchurl);
HttpEntity request = new HttpEntity(headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> response = restTemplate.exchange(
searchurl,
HttpMethod.GET,
request,
String.class
);
} catch (Exception e) {
e.printStackTrace();
}
但是如果没有从 REST-B 中找到客户(客户 API),那么我会得到
http://localhost:8086/customer-api/customers/customer-528f2331-d0c8-46f6-88c2-7445ee6f4821
Customer id received :: customer-528f2331-d0c8-46f6-88c2-7445ee6f4821
org.springframework.web.client.HttpClientErrorException: 404 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
如何从一个 springboot
应用程序调用休息端点到另一个应用程序并正确处理响应?
您可以从HttpClientErrorException
获取响应正文如下:
try {
ResponseEntity<String> response = restTemplate.exchange(
searchurl,
HttpMethod.GET,
request,
String.class
);
} catch (HttpClientErrorException e) {
String errorResponseBody = e.getResponseBodyAsString();
e.printStackTrace();
}
然后您可以使用 Jackson ObjectMapper
将字符串映射到 Java 对象。