This commit is contained in:
kimscott
2019-12-10 15:30:48 +09:00
parent 5635d9108c
commit 9eec049b6a
2 changed files with 47 additions and 7 deletions

View 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()));
}
}
}

View File

@@ -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<Order> process(Resource<Order> 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 {
};
}
}