У меня есть приложение Spring Boot, написанное на Java, которое является REST API. Эта служба (Sv c A) вызывает службу REST API (Sv c B), которая также является Spring Boot Application, написанным на Java. Sv c B возвращает код состояния 404, когда данные не найдены. Мне нужно изменить этот ответ на код состояния 200 и вернуть пустой объект ответа. Я не уверен, если или как это сделать.
Я могу поймать ошибку и определить, является ли 404 эта ошибка данных не найдено. Однако я не знаю, как изменить ответ на 200 пустых ответов. Я использую FeignClient для вызова службы. Это код ошибки, который ловит 404:
@Component
public class FeignErrorDecoder implements ErrorDecoder {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Exception decode(String methodKey, Response response) {
Reader reader = null;
String messageText = null;
switch (response.status()){
case 400:
logger.error("Status code " + response.status() + ", methodKey = " + methodKey);
case 404:
{
logger.error("Error took place when using Feign client to send HTTP Request. Status code " + response.status() + ", methodKey = " + methodKey);
try {
reader = response.body().asReader();
//Easy way to read the stream and get a String object
String result = CharStreams.toString(reader);
logger.error("RESPONSE BODY: " + result);
ObjectMapper mapper = new ObjectMapper();
//just in case you missed an attribute in the Pojo
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//init the Pojo
ExceptionMessage exceptionMessage = mapper.readValue(result,
ExceptionMessage.class);
messageText = exceptionMessage.getMessage();
logger.info("message: " + messageText);
} catch(IOException ex) {
logger.error(ex.getMessage());
}
finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new ResponseStatusException(HttpStatus.valueOf(200), messageText);
}
default:
return new Exception(response.reason());
}
}
}
Я могу изменить код состояния на 200, и он возвращает 200, но мне нужно, чтобы в ответе был пустой объект ответа. Приведенный выше код вернет это тело ответа объекта ответа об ошибке:
{
"statusCd" : "200",
"message" : "The Location not found for given Location Number and Facility Type Code",
"detailDesc" : "The Location not found for given Location Number and Facility Type Code. Error Timestamp : 2020-01-31 18:19:13"
}
Мне нужно, чтобы оно возвращало тело ответа, например: 200 - Пустой ответ
{
"facilityNumber": "923",
"facilityTimeZone": null,
"facilityAbbr": null,
"scheduledOperations": []
}