2
\$\begingroup\$

Here is my orchestration endpoint where I call customer endpoint and product endpoint. Product endpoint contains info about what customer have what product. I do not like how it look and smells as shit hope there is a better way to do it. Couldn't find a better way to do it but RestTemplate feels so wrong here.

@RestController
public class ApiController {
 private final static String CUSTOMER_URI = "http://localhost:8090/customers";
 private final static String PRODUCT_URI = "http://localhost:8085/products";
 private final static String PRODUCT_CUSTOM_URI = "http://localhost:8085/products/search/findByOwners";
 @GetMapping("/getCustomerById/{id}")
 public Customer getCustomerById( @PathVariable int id){
 MappingJackson2HttpMessageConverter converter = getMappingJackson2HttpMessageConverter();
 RestTemplate restTemplateHal = new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
 RestTemplate restTemplate = new RestTemplate();
 Customer customer = getCustomer(id, restTemplate);
 ArrayList<String> products = getProduct(id, restTemplateHal);
 customer.setProducts(products);
 customer.setId(id);
 return customer;
 }
 private ArrayList<String> getProduct(@PathVariable int id, RestTemplate restTemplateHal) {
 Collection content = restTemplateHal.getForObject(PRODUCT_CUSTOM_URI + "?id=" + id, PagedResources.class).getContent();
 ArrayList<String> products = new ArrayList<>();
 for (Object o : content) {
 System.out.println(o);
 LinkedHashMap<String, String> o1 = (LinkedHashMap<String, String>)o;
 products.add(o1.get("name"));
 }
 return products;
 }
 private Customer getCustomer(@PathVariable int id, RestTemplate restTemplate) {
 ResponseEntity<Customer> responseCustomer
 = restTemplate.getForEntity(CUSTOMER_URI + "/"+id, Customer.class);
 return responseCustomer.getBody();
 }
 private MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
 ObjectMapper mapper = new ObjectMapper();
 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 mapper.registerModule(new Jackson2HalModule());
 MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
 converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
 converter.setObjectMapper(mapper);
 return converter;
 }
}
asked Sep 11, 2017 at 14:42
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It's because your presentation layer does the orchestration. I'd recommend to make the rest layer as stupid as possible. Its only purpose should be to get the request information from the URL, load the corresponding data from the service layer, and transform the data to JSON or whatever format it needs to be.

answered Sep 13, 2017 at 12:05
\$\endgroup\$
2
  • \$\begingroup\$ the same mess would be in service \$\endgroup\$ Commented Sep 14, 2017 at 13:21
  • \$\begingroup\$ Well, what are you calling a mess? The orchestration code or the rest code? Delegating the orchestration to a layer below will get rid of the rest mess, not of the orchestration 'mess', which I assumed was your main concern, wasn't it? \$\endgroup\$ Commented Sep 14, 2017 at 15:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.