impl error handling

This commit is contained in:
Philipp Schweizer 2020-06-13 19:37:34 +02:00
parent ebeba72278
commit fa7bdf2f7e
2 changed files with 21 additions and 14 deletions

View File

@ -1,11 +1,13 @@
package org.hso.ecommerce.action.shop;
import org.hso.ecommerce.api.RestServiceForDelivery;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.springframework.web.client.ResourceAccessException;
public class EnableTrackingAction {
public static void addTrackingInfo(CustomerOrder customerOrder) {
// TODO:
customerOrder.trackingId = "555-NASE";
public static void addTrackingInfo(CustomerOrder customerOrder) throws ResourceAccessException {
customerOrder.trackingId = new RestServiceForDelivery().getDeliveryID(customerOrder);
}
}

View File

@ -1,8 +1,5 @@
package org.hso.ecommerce.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hso.ecommerce.controller.intern.customers.CustomerOrderController;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.springframework.beans.factory.annotation.Autowired;
@ -10,6 +7,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
@ -22,7 +20,7 @@ public class RestServiceForDelivery {
private final RestTemplateBuilder restTemplateBuilder = null;
public String getDeliveryID(CustomerOrder customerOrder)
public String getDeliveryID(CustomerOrder customerOrder) throws ResourceAccessException
{
String url = "http://[::1]:8082/newDelivery";
@ -44,7 +42,7 @@ public class RestServiceForDelivery {
{
return response.getBody();
} else {
return null;
throw new ResourceAccessException("Http Status wrong");
}
}
@ -61,15 +59,22 @@ public class RestServiceForDelivery {
HttpEntity<?> entity = new HttpEntity<>(headers);
ResponseEntity<CustomerOrderController.DeliveryData> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class);
ResponseEntity<CustomerOrderController.DeliveryData> response;
if (response.getStatusCode() == HttpStatus.OK)
try
{
return response.getBody();
} else {
return null;
response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class);
if (response.getStatusCode() == HttpStatus.OK)
{
return response.getBody();
} else {
return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----");
}
}
catch (ResourceAccessException e)
{
return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----");
}
}
}