-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloWorldController.java
56 lines (47 loc) · 1.61 KB
/
HelloWorldController.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
package com.kubra.rest;
import com.kubra.rest.models.Contact;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(
path = "hello",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public class HelloWorldController {
private MessageSource messageSource;
public HelloWorldController(MessageSource messageSource) {
this.messageSource = messageSource;
}
@GetMapping
public ResponseEntity helloAnonymous() {
Map<String, String> body = new HashMap<>();
body.put(i18n("greeting"), i18n("world"));
return ResponseEntity.ok(body);
}
private String i18n(String message) {
return messageSource.getMessage(message, null, LocaleContextHolder.getLocale());
}
@GetMapping("admin")
public ResponseEntity helloAdmin() {
Map<String, String> body = new HashMap<>();
body.put(i18n("greeting"), "admin");
return ResponseEntity.ok(body);
}
@GetMapping("user")
public ResponseEntity helloUser() {
Map<String, String> body = new HashMap<>();
body.put(i18n("greeting"), "user");
return ResponseEntity.ok(body);
}
}