/
GreeterController.java
45 lines (34 loc) · 1.34 KB
/
GreeterController.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
package com.tmp;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/api/greetings")
public class GreeterController {
private static final String template = "Hello, %s!";
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public @ResponseBody Greeting greetNoName() {
return new Greeting(String.format(template, "World"));
}
@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = { "application/json" })
public @ResponseBody Greeting greetName(final @PathVariable String name) {
return new Greeting(String.format(template, name));
}
public class Greeting {
private final String content;
private final LocalDateTime time;
public Greeting(String content) {
this.content = content;
this.time = LocalDateTime.now();
}
public String getContent() {
return this.content;
}
public String getTime() {
return this.time.toString();
}
}
}