Skip to content

Instantly share code, notes, and snippets.

@Leward
Created February 3, 2017 06:39
Show Gist options
  • Save Leward/f00ba19d480ede2cf8716383a3bd1895 to your computer and use it in GitHub Desktop.
Save Leward/f00ba19d480ede2cf8716383a3bd1895 to your computer and use it in GitHub Desktop.
hello ribbon
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.hateoas.Resources;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Optional;
@RestController
public class GreetingController {
private static class AlbumResources extends Resources<Album> { }
private String greeting;
private final RestTemplate restTemplate;
@Autowired
public GreetingController(RestTemplate restTemplate, @Value("${greeting:Hello unknown}") String greeting) {
this.greeting = greeting;
this.restTemplate = restTemplate;
}
@GetMapping("/")
public String greet() {
String url = "http://zenika-music/albums";
Optional<AlbumResources> albums;
try {
albums = Optional.of(restTemplate.getForObject(url, AlbumResources.class));
} catch (Exception e) {
albums = Optional.empty();
}
String albumsSummary = albums.map(it -> "There are " + it.getContent().size() + " albums. ")
.orElse("We don't know how many albums there are. ");
return greeting + albumsSummary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment