-
Notifications
You must be signed in to change notification settings - Fork 51
/
GreeterApplication.java
67 lines (52 loc) · 2.11 KB
/
GreeterApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package greeter;
import java.net.URI;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class GreeterApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate rest;
public static void main(String[] args) {
SpringApplication.run(GreeterApplication.class, args);
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam(value="salutation", defaultValue="Hello") String salutation, @RequestParam(value="name", defaultValue="Bob") String name) {
URI uri = UriComponentsBuilder.fromUriString("http://message-generation/greeting")
.queryParam("salutation", salutation)
.queryParam("name", name)
.build()
.toUri();
Greeting greeting = rest.getForObject(uri, Greeting.class);
return greeting.getMessage();
}
private static class Greeting {
private String message;
@JsonCreator
public Greeting(@JsonProperty("message") String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}
}