diff --git a/src/main/java/com/example/template/OrderController.java b/src/main/java/com/example/template/OrderController.java new file mode 100644 index 0000000..724fe9a --- /dev/null +++ b/src/main/java/com/example/template/OrderController.java @@ -0,0 +1,38 @@ +package com.example.template; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +@RestController +public class OrderController { + + private static final String RESPONSE_STRING_FORMAT = "order => %s\n"; + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + RestTemplate restTemplate; + + @Value("${delivery.api.url:http://delivery:8080/startDelivery}") + private String remoteURL; + + @GetMapping("/order") + ResponseEntity fakeOrder() { + try { + ResponseEntity responseEntity = restTemplate.getForEntity(remoteURL, String.class); + String response = responseEntity.getBody(); + return ResponseEntity.ok(String.format(RESPONSE_STRING_FORMAT, response.trim())); + } catch (Exception ex) { + logger.warn("Exception trying to get the response from order service.", ex); + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE) + .body(String.format(RESPONSE_STRING_FORMAT, ex.getMessage())); + } + } +} diff --git a/src/main/java/com/example/template/config/Config.java b/src/main/java/com/example/template/config/Config.java index 03e166f..8336cb0 100644 --- a/src/main/java/com/example/template/config/Config.java +++ b/src/main/java/com/example/template/config/Config.java @@ -2,14 +2,14 @@ package com.example.template.config; import com.example.template.Order; import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.hateoas.Link; -import org.springframework.hateoas.Resource; -import org.springframework.hateoas.ResourceProcessor; +import org.springframework.hateoas.*; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; +import javax.xml.ws.soap.Addressing; import java.net.MalformedURLException; import java.net.URL; @@ -17,6 +17,9 @@ import java.net.URL; @Configuration public class Config { + @Autowired + EntityLinks entityLinks; + @Bean RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); @@ -34,11 +37,9 @@ public class Config { @Override public Resource process(Resource resource) { - Link selfLink = resource.getLink("self"); - String selfLinkUrl = selfLink.getHref(); + LinkBuilder linkBuilder = entityLinks.linkFor(Order.class); try { - URL url = new URL(selfLinkUrl); - + URL url = new URL(linkBuilder.withSelfRel().getHref()); resource.add(new Link(url.getProtocol() + "://" + url.getHost() + ":" + url.getPort() + "/deliveries/search/findByOrderIdOrderByDeliveryIdDesc?orderId=" + resource.getContent().getId(), "delivery")); } catch (MalformedURLException e) { @@ -50,3 +51,4 @@ public class Config { }; } } +