.
This commit is contained in:
38
src/main/java/com/example/template/OrderController.java
Normal file
38
src/main/java/com/example/template/OrderController.java
Normal file
@@ -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<String> fakeOrder() {
|
||||||
|
try {
|
||||||
|
ResponseEntity<String> 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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,14 @@ package com.example.template.config;
|
|||||||
|
|
||||||
import com.example.template.Order;
|
import com.example.template.Order;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.hateoas.Link;
|
import org.springframework.hateoas.*;
|
||||||
import org.springframework.hateoas.Resource;
|
|
||||||
import org.springframework.hateoas.ResourceProcessor;
|
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.xml.ws.soap.Addressing;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
@@ -17,6 +17,9 @@ import java.net.URL;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
EntityLinks entityLinks;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
RestTemplate restTemplate() {
|
RestTemplate restTemplate() {
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
@@ -34,11 +37,9 @@ public class Config {
|
|||||||
@Override
|
@Override
|
||||||
public Resource<Order> process(Resource<Order> resource) {
|
public Resource<Order> process(Resource<Order> resource) {
|
||||||
|
|
||||||
Link selfLink = resource.getLink("self");
|
LinkBuilder linkBuilder = entityLinks.linkFor(Order.class);
|
||||||
String selfLinkUrl = selfLink.getHref();
|
|
||||||
try {
|
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"));
|
resource.add(new Link(url.getProtocol() + "://" + url.getHost() + ":" + url.getPort() + "/deliveries/search/findByOrderIdOrderByDeliveryIdDesc?orderId=" + resource.getContent().getId(), "delivery"));
|
||||||
|
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
@@ -50,3 +51,4 @@ public class Config {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user